Skip to content

Instantly share code, notes, and snippets.

@yantene
Last active February 17, 2019 13:34
Show Gist options
  • Save yantene/c159dd992b2a4b392d147666f9688eb9 to your computer and use it in GitHub Desktop.
Save yantene/c159dd992b2a4b392d147666f9688eb9 to your computer and use it in GitHub Desktop.
10つくったー (https://shindanmaker.com/217387) ソルバ
#!/usr/bin/env ruby
# ./ju_solver.rb 13 9 -3 73
ARGV.map(&:to_i).then { |ord|
%i[+ - * /].repeated_combination(ord.size - 1).flat_map { |orr|
(ord + orr).permutation((ord + orr).size).map { |formula|
begin
formula if formula.each.with_object([]) { |v, obj|
if v.is_a? Symbol
fail if obj.size < 2 || v == :/ && obj[-1].zero?
obj[-2, 2] = obj[-2].send(v, obj[-1])
else
obj << v.to_r
end
}.first == 10
rescue
end
}
}
}.compact.each do |formula|
puts formula.each.with_object([]) { |v, obj|
if v.is_a? Symbol
obj[-2, 2] = "(#{obj[-2, 2].join(" #{v} ")})"
else
obj << v
end
}[0]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment