18 "output_type": "execute_result"
22 "load 'array-numbers.rb'"
26 "cell_type": "markdown",
29 "* d2d3d4=406 is divisible by 2\n",
30 "* d3d4d5=063 is divisible by 3\n",
31 "* d4d5d6=635 is divisible by 5\n",
32 "* d5d6d7=357 is divisible by 7\n",
33 "* d6d7d8=572 is divisible by 11\n",
34 "* d7d8d9=728 is divisible by 13\n",
35 "* d8d9d10=289 is divisible by 17"
53 "output_type": "execute_result"
59 " items.each_index do |i|\n",
60 " plucked << [items[i], items[0, i] + items[i+1..-1]]\n",
76 "[[0, [1, 2, 3, 4]], [1, [0, 2, 3, 4]], [2, [0, 1, 3, 4]], [3, [0, 1, 2, 4]], [4, [0, 1, 2, 3]]]"
81 "output_type": "execute_result"
101 "execution_count": 4,
103 "output_type": "execute_result"
107 "def choices(partial_solution)\n",
109 " pluck(partial_solution[:remaining]).each do |digit, remaining|\n",
110 " choices << {digits: partial_solution[:digits] + [digit], remaining: remaining}\n",
118 "execution_count": 5,
126 "{:digits=>[], :remaining=>[0, 1, 2, 3]}"
129 "execution_count": 5,
131 "output_type": "execute_result"
135 "initial = {digits: [], remaining: (0..3).to_a}"
140 "execution_count": 6,
148 "[{:digits=>[0], :remaining=>[1, 2, 3]}, {:digits=>[1], :remaining=>[0, 2, 3]}, {:digits=>[2], :remaining=>[0, 1, 3]}, {:digits=>[3], :remaining=>[0, 1, 2]}]"
151 "execution_count": 6,
153 "output_type": "execute_result"
162 "execution_count": 7,
170 "[{:digits=>[0, 1], :remaining=>[2, 3]}, {:digits=>[0, 2], :remaining=>[1, 3]}, {:digits=>[0, 3], :remaining=>[1, 2]}, {:digits=>[1, 0], :remaining=>[2, 3]}, {:digits=>[1, 2], :remaining=>[0, 3]}, {:digits=>[1, 3], :remaining=>[0, 2]}, {:digits=>[2, 0], :remaining=>[1, 3]}, {:digits=>[2, 1], :remaining=>[0, 3]}, {:digits=>[2, 3], :remaining=>[0, 1]}, {:digits=>[3, 0], :remaining=>[1, 2]}, {:digits=>[3, 1], :remaining=>[0, 2]}, {:digits=>[3, 2], :remaining=>[0, 1]}]"
173 "execution_count": 7,
175 "output_type": "execute_result"
179 "choices(initial).flat_map {|s| choices(s)}"
184 "execution_count": 8,
195 "execution_count": 8,
197 "output_type": "execute_result"
202 " initial = {digits: [], remaining: (0..9).to_a}\n",
203 " d0s = choices(initial)\n",
204 " d1s = d0s.flat_map {|s| choices(s)}\n",
205 " d2s = d1s.flat_map {|s| choices(s)}\n",
206 " d3s = d2s.flat_map {|s| choices(s)}.select {|s| s[:digits][-3..-1].to_i % 2 == 0}\n",
207 " d4s = d3s.flat_map {|s| choices(s)}.select {|s| s[:digits][-3..-1].to_i % 3 == 0}\n",
208 " d5s = d4s.flat_map {|s| choices(s)}.select {|s| s[:digits][-3..-1].to_i % 5 == 0}\n",
209 " d6s = d5s.flat_map {|s| choices(s)}.select {|s| s[:digits][-3..-1].to_i % 7 == 0}\n",
210 " d7s = d6s.flat_map {|s| choices(s)}.select {|s| s[:digits][-3..-1].to_i % 11 == 0}\n",
211 " d8s = d7s.flat_map {|s| choices(s)}.select {|s| s[:digits][-3..-1].to_i % 13 == 0}\n",
212 " d9s = d8s.flat_map {|s| choices(s)}.select {|s| s[:digits][-3..-1].to_i % 17 == 0}\n",
213 " # puts \"0: #{d0s.length}, 1: #{d1s.length}, 2: #{d2s.length}, 3: #{d3s.length}, 4: #{d4s.length}, 5: #{d5s.length}, 6: #{d6s.length}, 7: #{d7s.length}, 8: #{d8s.length}, 9: #{d9s.length}\"\n",
214 " d9s.map {|s| s[:digits].to_i}.sum\n",
220 "execution_count": 9,
231 "execution_count": 9,
233 "output_type": "execute_result"
237 "def solve_step(partial_solutions, divisor)\n",
238 " solns = partial_solutions.flat_map {|s| choices(s)}\n",
239 " if divisor == 1\n",
242 " solns.select {|s| s[:digits][-3..-1].to_i % divisor == 0}\n",
249 "execution_count": 10,
260 "execution_count": 10,
262 "output_type": "execute_result"
267 " initial = {digits: [], remaining: (0..9).to_a}\n",
268 " solutions = choices(initial)\n",
269 " [1, 1, 2, 3, 5, 7, 11, 13, 17].each do |d|\n",
270 " # puts \"#{d}, #{solutions.length}, #{solutions[0][:digits].length}\"\n",
271 " solutions = solve_step(solutions, d)\n",
273 " solutions.map {|s| s[:digits].to_i}.sum\n",
279 "execution_count": 11,
290 "execution_count": 11,
292 "output_type": "execute_result"
301 "execution_count": 12,
312 "execution_count": 12,
314 "output_type": "execute_result"
323 "execution_count": null,
333 "display_name": "Ruby 2.4.0",
338 "file_extension": ".rb",
339 "mimetype": "application/x-ruby",