Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generates all possible football goal sequences leading to a score of 21
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