casey/tree_solve.rb
2023-05-09 21:20:50 -04:00

50 lines
788 B
Ruby

# frozen_string_literal: true
require 'cas'
require 'rule'
require 'rw_rules'
require 'simp_rules'
# sucks
class Solver
def initialize(solve_for: :x, given: [])
@solve_for = solve_for
@visited = MySet.new([given])
end
def winner
found = @visited.find { |g| g.lhs == @solve_for || g.rhs == @solve_for && g.lhs != g.rhs }
return unless found
simp(if found.lhs == @solve_for
found.rhs
else
found.lhs
end)
end
def elaborate!
@visited.each do |t|
(SIMP_RULES + RW_RULES).each do |rule|
@visited.add(rule.apply(t))
end
end
simp!
end
def simp!
@visited.map! { |g| g.simp }
self
end
end
class MySet < Set
def add(item)
super.add(item) unless any? { |i| item == i }
self
end
end