Skip to content

Instantly share code, notes, and snippets.

@jedisct1
Created April 5, 2011 18:17
Show Gist options
  • Save jedisct1/904172 to your computer and use it in GitHub Desktop.
Save jedisct1/904172 to your computer and use it in GitHub Desktop.
My attempt at solving the Greplin puzzle #3
SET = [ 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99 ]
def find_with(set, target, expr = [ ], res = [ ])
lowset = set.select { |e| e <= target }
lowset.each do |n|
expr2 = expr + [n]
if n == target
res << "#{expr2.sort.join(' + ')} = #{expr2.inject(:+)}" unless expr.empty?
else
find_with(lowset - [n], target - n, expr2, res)
end
end
res
end
SET.each { |n| puts find_with(SET, n).uniq }
@jedisct1
Copy link
Author

jedisct1 commented Apr 5, 2011

4 + 15 = 19
4 + 9 + 15 = 28
9 + 19 = 28
3 + 15 + 19 = 37
4 + 14 + 19 = 37
9 + 28 = 37
4 + 9 + 15 + 19 = 47
4 + 15 + 28 = 47
19 + 28 = 47
3 + 4 + 9 + 15 + 19 = 50
3 + 4 + 15 + 28 = 50
3 + 19 + 28 = 50
3 + 47 = 50
4 + 9 + 37 = 50
3 + 4 + 19 + 28 = 54
3 + 4 + 47 = 54
3 + 9 + 14 + 28 = 54
3 + 14 + 37 = 54
4 + 50 = 54
4 + 9 + 15 + 28 = 56
4 + 15 + 37 = 56
9 + 19 + 28 = 56
9 + 47 = 56
19 + 37 = 56
3 + 4 + 9 + 15 + 28 = 59
3 + 4 + 15 + 37 = 59
3 + 9 + 19 + 28 = 59
3 + 9 + 47 = 59
3 + 19 + 37 = 59
3 + 56 = 59
9 + 50 = 59
3 + 4 + 54 = 61
4 + 9 + 14 + 15 + 19 = 61
4 + 14 + 15 + 28 = 61
9 + 15 + 37 = 61
14 + 19 + 28 = 61
14 + 47 = 61
3 + 4 + 9 + 54 = 70
4 + 9 + 14 + 15 + 28 = 70
4 + 14 + 15 + 37 = 70
4 + 19 + 47 = 70
9 + 14 + 19 + 28 = 70
9 + 14 + 47 = 70
9 + 61 = 70
14 + 19 + 37 = 70
14 + 56 = 70
3 + 4 + 9 + 14 + 15 + 28 = 73
3 + 4 + 14 + 15 + 37 = 73
3 + 4 + 19 + 47 = 73
3 + 9 + 14 + 19 + 28 = 73
3 + 9 + 14 + 47 = 73
3 + 9 + 61 = 73
3 + 14 + 19 + 37 = 73
3 + 14 + 56 = 73
3 + 70 = 73
4 + 15 + 54 = 73
4 + 19 + 50 = 73
9 + 14 + 50 = 73
14 + 59 = 73
19 + 54 = 73
3 + 4 + 9 + 15 + 19 + 28 = 78
3 + 4 + 9 + 15 + 47 = 78
3 + 4 + 15 + 19 + 37 = 78
3 + 4 + 15 + 56 = 78
3 + 9 + 14 + 15 + 37 = 78
3 + 9 + 19 + 47 = 78
3 + 14 + 61 = 78
3 + 19 + 56 = 78
3 + 28 + 47 = 78
4 + 9 + 15 + 50 = 78
4 + 9 + 28 + 37 = 78
4 + 15 + 59 = 78
9 + 15 + 54 = 78
9 + 19 + 50 = 78
19 + 59 = 78
28 + 50 = 78
3 + 4 + 9 + 15 + 50 = 81
3 + 4 + 9 + 28 + 37 = 81
3 + 4 + 15 + 59 = 81
3 + 9 + 15 + 54 = 81
3 + 9 + 19 + 50 = 81
3 + 19 + 59 = 81
3 + 28 + 50 = 81
3 + 78 = 81
4 + 9 + 14 + 54 = 81
15 + 19 + 47 = 81
3 + 4 + 9 + 14 + 15 + 19 + 28 = 92
3 + 4 + 9 + 14 + 15 + 47 = 92
3 + 4 + 9 + 15 + 61 = 92
3 + 4 + 14 + 15 + 19 + 37 = 92
3 + 4 + 14 + 15 + 56 = 92
3 + 4 + 15 + 70 = 92
3 + 9 + 14 + 19 + 47 = 92
3 + 9 + 15 + 28 + 37 = 92
3 + 9 + 19 + 61 = 92
3 + 14 + 19 + 56 = 92
3 + 14 + 28 + 47 = 92
3 + 19 + 70 = 92
3 + 28 + 61 = 92
4 + 9 + 14 + 15 + 50 = 92
4 + 9 + 14 + 28 + 37 = 92
4 + 14 + 15 + 59 = 92
4 + 15 + 19 + 54 = 92
4 + 15 + 73 = 92
9 + 14 + 15 + 54 = 92
9 + 14 + 19 + 50 = 92
14 + 19 + 59 = 92
14 + 28 + 50 = 92
14 + 78 = 92
19 + 73 = 92
3 + 4 + 9 + 14 + 15 + 50 = 95
3 + 4 + 9 + 14 + 28 + 37 = 95
3 + 4 + 14 + 15 + 59 = 95
3 + 4 + 15 + 19 + 54 = 95
3 + 4 + 15 + 73 = 95
3 + 9 + 14 + 15 + 54 = 95
3 + 9 + 14 + 19 + 50 = 95
3 + 14 + 19 + 59 = 95
3 + 14 + 28 + 50 = 95
3 + 14 + 78 = 95
3 + 19 + 73 = 95
3 + 92 = 95
4 + 9 + 28 + 54 = 95
4 + 37 + 54 = 95
14 + 15 + 19 + 47 = 95
14 + 81 = 95
15 + 19 + 61 = 95
3 + 4 + 9 + 15 + 19 + 47 = 97
3 + 4 + 9 + 81 = 97
3 + 4 + 14 + 15 + 61 = 97
3 + 4 + 15 + 19 + 56 = 97
3 + 4 + 15 + 28 + 47 = 97
3 + 9 + 14 + 15 + 19 + 37 = 97
3 + 9 + 14 + 15 + 56 = 97
3 + 9 + 15 + 70 = 97
3 + 14 + 15 + 28 + 37 = 97
3 + 14 + 19 + 61 = 97
3 + 19 + 28 + 47 = 97
4 + 9 + 14 + 70 = 97
4 + 9 + 15 + 19 + 50 = 97
4 + 9 + 19 + 28 + 37 = 97
4 + 9 + 28 + 56 = 97
4 + 9 + 37 + 47 = 97
4 + 15 + 19 + 59 = 97
4 + 15 + 28 + 50 = 97
4 + 15 + 78 = 97
4 + 37 + 56 = 97
9 + 14 + 15 + 59 = 97
9 + 15 + 19 + 54 = 97
9 + 15 + 73 = 97
15 + 28 + 54 = 97
19 + 28 + 50 = 97
19 + 78 = 97
47 + 50 = 97
3 + 4 + 9 + 14 + 15 + 54 = 99
3 + 4 + 9 + 14 + 19 + 50 = 99
3 + 4 + 14 + 19 + 59 = 99
3 + 4 + 14 + 28 + 50 = 99
3 + 4 + 14 + 78 = 99
3 + 4 + 19 + 73 = 99
3 + 4 + 92 = 99
3 + 9 + 14 + 19 + 54 = 99
3 + 9 + 14 + 73 = 99
3 + 9 + 28 + 59 = 99
3 + 9 + 37 + 50 = 99
3 + 14 + 28 + 54 = 99
3 + 15 + 81 = 99
3 + 37 + 59 = 99
4 + 14 + 15 + 19 + 47 = 99
4 + 14 + 81 = 99
4 + 15 + 19 + 61 = 99
4 + 95 = 99
9 + 14 + 15 + 61 = 99
9 + 15 + 19 + 56 = 99
9 + 15 + 28 + 47 = 99
14 + 15 + 70 = 99
15 + 19 + 28 + 37 = 99
15 + 28 + 56 = 99
15 + 37 + 47 = 99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment