Created
October 23, 2013 20:17
-
-
Save marksim/7125937 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Equation | |
def initialize(*args) | |
@argset = args | |
end | |
attr_reader :argset | |
def sum | |
if @argset.length == 1 | |
return @argset.first | |
else | |
first = Equation.new(*@argset[0..-3]).sum | |
operator = @argset[-2] | |
second = @argset[-1] | |
first.send(operator, second) | |
end | |
end | |
def to_s | |
@argset.map(&:to_s).join(" ") + " = #{sum}" | |
end | |
include Comparable | |
def <=> (o) | |
@argset <=> o.argset | |
end | |
end | |
class Solver | |
attr_reader :numbers | |
def initialize(*numbers) | |
@numbers = numbers | |
end | |
def solve | |
possible_numbers.each do |numbers| | |
inject_operators(numbers, [:+, :-, :-]).each do |equation| | |
yield equation | |
end | |
end | |
end | |
def possible_numbers | |
s = numbers.join("") | |
s = s.insert(1, "|").insert(3,"|").insert(5, "|") | |
rotate_pipes(s).map {|m| m.split("|").map(&:to_i)}.uniq | |
end | |
def inject_operators(numbers, operators) | |
operators.permutation.to_a.uniq.map do |operator_set| | |
Equation.new *(numbers.zip(operator_set).flatten.compact) | |
end | |
end | |
private | |
def rotate_pipes(s) | |
s.scan(/\|\d\d+/).map {|m| s.sub(m, m[0, 2].reverse.to_s + m[2..-1].to_s)}. | |
map {|r| rotate_pipes(r)}.flatten + [s] | |
end | |
end | |
describe "Quiz 119" do | |
it "knows when the answer is 100" do | |
expect(Equation.new(25, :+, 25, :+, 56, :-, 6).sum).to eql 100 | |
end | |
it "can tell the answer to an equation" do | |
expect(Equation.new(10, :+, 20).sum).to eq(30) | |
expect(Equation.new(1, :+, 2, :-, 3, :-, 456789).sum).to eq(-456789) | |
expect(Equation.new(1, :+, 23, :-, 4567, :-, 89).sum).to eq(-4632) | |
end | |
it "can return all possible equations for a set of numbers" do | |
equations = Solver.new(1).inject_operators([1, 2, 3, 4], [:-, :-, :+]) | |
expect(equations.count).to eql 3 | |
expect(equations).to include Equation.new(1, :-, 2, :-, 3, :+, 4) | |
end | |
describe "returns all possible sets of 4 numbers for a given sent of digits" do | |
it "splits 1, 2, 3, 4 into one result" do | |
expect(Solver.new(1, 2, 3, 4).possible_numbers).to eql [[1, 2, 3, 4]] | |
end | |
it "splits 1, 2, 3, 4, 5 into 4 results" do | |
expect(Solver.new(1, 2, 3, 4, 5).possible_numbers.count).to eql 4 | |
end | |
it "splits 1, 2, 3, 4, 5, 6 into 9 results" do | |
expect(Solver.new(1, 2, 3, 4, 5, 6).possible_numbers.count).to eql 10 | |
end | |
it "splits 1, 2, 3, 4, 5, 6, 7, 8, 9 into 36 results" do | |
expect(Solver.new(1, 2, 3, 4, 5, 6, 7, 8, 9).possible_numbers.count).to eql 56 | |
end | |
end | |
end | |
Solver.new(1, 2, 3, 4, 5, 6, 7, 8, 9).solve do |equation| | |
if equation.sum == 100 | |
puts "******************************************" | |
puts equation | |
puts "******************************************" | |
else | |
puts equation | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment