Created
June 16, 2015 21:03
-
-
Save timraymond/0ae0d02ef285c22a0152 to your computer and use it in GitHub Desktop.
Generates all possible football goal sequences leading to a score of 21
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
def football(results, trace, sum, roots, nodes) | |
results_p = results.dup | |
if sum < 0 | |
return results_p | |
elsif sum == 0 | |
return results_p << trace | |
else | |
nodes_p = nodes.dup | |
roots.each {|rt| nodes_p << rt } # push each root on the node stack | |
while r = nodes_p.pop | |
results_p = results_p + football(results, trace + [r], sum - r, roots, nodes_p) | |
end | |
results_p | |
end | |
end | |
football([], [], 21, [2, 3, 7], []).each {|trace| p [trace, trace.inject(0, :+)]} | |
# --SNIP-- | |
# [[2, 2, 2, 2, 2, 2, 2, 7], 21] | |
# [[2, 2, 2, 2, 2, 2, 2, 3, 2, 2], 21] | |
# [[2, 2, 2, 2, 2, 2, 2, 3, 2, 2], 21] | |
# [[2, 2, 2, 2, 2, 2, 2, 3, 2, 2], 21] | |
# [[2, 2, 2, 2, 2, 2, 2, 2, 3, 2], 21] | |
# [[2, 2, 2, 2, 2, 2, 2, 2, 3, 2], 21] | |
# [[2, 2, 2, 2, 2, 2, 2, 2, 2, 3], 21] | |
# 6219 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment