Last active
April 29, 2020 16:24
-
-
Save peterc/0b4db6eda7d18bea3513289bf8724d28 to your computer and use it in GitHub Desktop.
Basic Countdown numbers solver in Ruby
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
# Basic Countdown numbers solver | |
# Principally ported from Jake Archibald's JS solution | |
# at https://jsbin.com/yimejin/4/edit?js,console | |
def solve(nums, target) | |
bs = nil | |
nums.sort.reverse.combination(2).each do |i, j| | |
%I{+ - * /}.each do |op| | |
next if op == :* && (i == 1 || j == 1) | |
next if op == :/ && (j == 1 || i % j != 0) | |
res = i.send(op, j) | |
next if op == :- && res == 0 | |
return "#{i} #{op} #{j} = #{res}" if res == target | |
nn = nums.dup + [res] | |
nn.delete_at(nn.index(i)) | |
nn.delete_at(nn.index(j)) | |
if ss = solve(nn, target) | |
bs = "#{i} #{op} #{j} = #{res}\n#{ss}" | |
end | |
end | |
end | |
bs | |
end | |
target = Random.new.rand(100..999) | |
nums = [25, 50, 75, 100].sample(2) + | |
(Array(1..10) * 2).sample(4) | |
puts "Goal: Reach #{target} with #{nums}" | |
puts solve(nums, target) || "No solution" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't find the steps ever really get out of control, so didn't feel the need to go for the purest lowest step solution.