Done puzzle 64
[project-euler.git] / euler61.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 2,
6 "metadata": {},
7 "outputs": [
8 {
9 "data": {
10 "text/plain": [
11 "true"
12 ]
13 },
14 "execution_count": 2,
15 "metadata": {},
16 "output_type": "execute_result"
17 }
18 ],
19 "source": [
20 "require_relative 'array-numbers'"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": 3,
26 "metadata": {},
27 "outputs": [
28 {
29 "data": {
30 "text/plain": [
31 ":valid_set"
32 ]
33 },
34 "execution_count": 3,
35 "metadata": {},
36 "output_type": "execute_result"
37 }
38 ],
39 "source": [
40 "def valid_set(&block)\n",
41 " (1..150).map {|i| block.call i}.select {|i| i >= 1000 && i <= 10000 && i % 10 != 0}\n",
42 "end"
43 ]
44 },
45 {
46 "cell_type": "code",
47 "execution_count": 4,
48 "metadata": {},
49 "outputs": [
50 {
51 "data": {
52 "text/plain": [
53 "[[1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431, 1485, 1596, 1653, 1711, 1891, 1953, 2016, 2145, 2211, 2278, 2346, 2415, 2485, 2556, 2628, 2701, 2775, 2926, 3003, 3081, 3321, 3403, 3486, 3655, 3741, 3828, 3916, 4005, 4095, 4186, 4278, 4371, 4465, 4656, 4753, 4851, 5151, 5253, 5356, 5565, 5671, 5778, 5886, 5995, 6105, 6216, 6328, 6441, 6555, 6786, 6903, 7021, 7381, 7503, 7626, 7875, 8001, 8128, 8256, 8385, 8515, 8646, 8778, 8911, 9045, 9316, 9453, 9591], [1001, 1162, 1247, 1335, 1426, 1617, 1717, 1926, 2035, 2147, 2262, 2501, 2625, 2752, 2882, 3015, 3151, 3432, 3577, 3725, 3876, 4187, 4347, 4676, 4845, 5017, 5192, 5551, 5735, 5922, 6112, 6305, 6501, 6902, 7107, 7315, 7526, 7957, 8177, 8626, 8855, 9087, 9322, 9801], [1071, 1177, 1288, 1404, 1525, 1651, 1782, 1918, 2059, 2205, 2356, 2512, 2673, 2839, 3186, 3367, 3553, 3744, 4141, 4347, 4558, 4774, 4995, 5221, 5452, 5688, 5929, 6175, 6426, 6682, 6943, 7209, 7756, 8037, 8323, 8614, 9211, 9517, 9828], [1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801], [1035, 1128, 1225, 1326, 1431, 1653, 1891, 2016, 2145, 2278, 2415, 2556, 2701, 3003, 3321, 3486, 3655, 3828, 4005, 4186, 4371, 4753, 5151, 5356, 5565, 5778, 5995, 6216, 6441, 6903, 7381, 7626, 7875, 8128, 8385, 8646, 8911, 9453], [1045, 1281, 1408, 1541, 1825, 1976, 2133, 2296, 2465, 2821, 3008, 3201, 3605, 3816, 4033, 4256, 4485, 4961, 5208, 5461, 5985, 6256, 6533, 6816, 7105, 7701, 8008, 8321, 8965, 9296, 9633, 9976]]"
54 ]
55 },
56 "execution_count": 4,
57 "metadata": {},
58 "output_type": "execute_result"
59 }
60 ],
61 "source": [
62 "triangles = valid_set {|n| n * (n + 1) / 2}\n",
63 "pentagonals = valid_set {|n| n * (3 * n - 1) / 2}\n",
64 "heptagonals = valid_set {|n| n * (5 * n - 3) / 2}\n",
65 "squares = valid_set {|n| n * n}\n",
66 "hexagonals = valid_set {|n| n * (2 * n - 1)}\n",
67 "octagonals = valid_set {|n| n * (3 * n - 2)}\n",
68 "[triangles, pentagonals, heptagonals, squares, hexagonals, octagonals]"
69 ]
70 },
71 {
72 "cell_type": "code",
73 "execution_count": 5,
74 "metadata": {},
75 "outputs": [
76 {
77 "data": {
78 "text/plain": [
79 ":connects"
80 ]
81 },
82 "execution_count": 5,
83 "metadata": {},
84 "output_type": "execute_result"
85 }
86 ],
87 "source": [
88 "def connects(n1, n2)\n",
89 " n1.to_digits[2..3] == n2.to_digits[0..1]\n",
90 "end"
91 ]
92 },
93 {
94 "cell_type": "code",
95 "execution_count": 6,
96 "metadata": {},
97 "outputs": [
98 {
99 "data": {
100 "text/plain": [
101 "true"
102 ]
103 },
104 "execution_count": 6,
105 "metadata": {},
106 "output_type": "execute_result"
107 }
108 ],
109 "source": [
110 "connects(1234, 3456)"
111 ]
112 },
113 {
114 "cell_type": "code",
115 "execution_count": 7,
116 "metadata": {},
117 "outputs": [
118 {
119 "data": {
120 "text/plain": [
121 "false"
122 ]
123 },
124 "execution_count": 7,
125 "metadata": {},
126 "output_type": "execute_result"
127 }
128 ],
129 "source": [
130 "connects(1234, 1234)"
131 ]
132 },
133 {
134 "cell_type": "code",
135 "execution_count": 8,
136 "metadata": {},
137 "outputs": [
138 {
139 "data": {
140 "text/plain": [
141 "{{:number=>1035, :type=>3}=>[{:number=>3577, :type=>5}, {:number=>3553, :type=>7}], {:number=>1081, :type=>3}=>[{:number=>8177, :type=>5}, {:number=>8128, :type=>6}], {:number=>1128, :type=>3}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>1176, :type=>3}=>[{:number=>7626, :type=>6}], {:number=>1225, :type=>3}=>[{:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>1275, :type=>3}=>[{:number=>7569, :type=>4}, {:number=>7526, :type=>5}], {:number=>1326, :type=>3}=>[{:number=>2601, :type=>4}, {:number=>2625, :type=>5}, {:number=>2673, :type=>7}], {:number=>1378, :type=>3}=>[{:number=>7875, :type=>6}], {:number=>1431, :type=>3}=>[{:number=>3136, :type=>4}, {:number=>3151, :type=>5}, {:number=>3186, :type=>7}], {:number=>1485, :type=>3}=>[], {:number=>1596, :type=>3}=>[{:number=>9604, :type=>4}, {:number=>9633, :type=>8}], {:number=>1653, :type=>3}=>[{:number=>5329, :type=>4}, {:number=>5356, :type=>6}], {:number=>1711, :type=>3}=>[{:number=>1156, :type=>4}, {:number=>1162, :type=>5}, {:number=>1128, :type=>6}, {:number=>1177, :type=>7}], {:number=>1891, :type=>3}=>[], {:number=>1953, :type=>3}=>[{:number=>5329, :type=>4}, {:number=>5356, :type=>6}], {:number=>2016, :type=>3}=>[{:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>2145, :type=>3}=>[{:number=>4558, :type=>7}], {:number=>2211, :type=>3}=>[{:number=>1156, :type=>4}, {:number=>1162, :type=>5}, {:number=>1128, :type=>6}, {:number=>1177, :type=>7}], {:number=>2278, :type=>3}=>[{:number=>7875, :type=>6}], {:number=>2346, :type=>3}=>[{:number=>4624, :type=>4}, {:number=>4676, :type=>5}], {:number=>2415, :type=>3}=>[{:number=>1521, :type=>4}, {:number=>1525, :type=>7}, {:number=>1541, :type=>8}], {:number=>2485, :type=>3}=>[], {:number=>2556, :type=>3}=>[{:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>2628, :type=>3}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>2701, :type=>3}=>[], {:number=>2775, :type=>3}=>[{:number=>7569, :type=>4}, {:number=>7526, :type=>5}], {:number=>2926, :type=>3}=>[{:number=>2601, :type=>4}, {:number=>2625, :type=>5}, {:number=>2673, :type=>7}], {:number=>3003, :type=>3}=>[], {:number=>3081, :type=>3}=>[{:number=>8177, :type=>5}, {:number=>8128, :type=>6}], {:number=>3321, :type=>3}=>[{:number=>2116, :type=>4}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}, {:number=>2133, :type=>8}], {:number=>3403, :type=>3}=>[], {:number=>3486, :type=>3}=>[{:number=>8649, :type=>4}, {:number=>8626, :type=>5}, {:number=>8646, :type=>6}, {:number=>8614, :type=>7}], {:number=>3655, :type=>3}=>[{:number=>5551, :type=>5}, {:number=>5565, :type=>6}], {:number=>3741, :type=>3}=>[{:number=>4187, :type=>5}, {:number=>4186, :type=>6}, {:number=>4141, :type=>7}], {:number=>3828, :type=>3}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>3916, :type=>3}=>[{:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>4005, :type=>3}=>[], {:number=>4095, :type=>3}=>[{:number=>9517, :type=>7}], {:number=>4186, :type=>3}=>[{:number=>8649, :type=>4}, {:number=>8626, :type=>5}, {:number=>8646, :type=>6}, {:number=>8614, :type=>7}], {:number=>4278, :type=>3}=>[{:number=>7875, :type=>6}], {:number=>4371, :type=>3}=>[{:number=>7107, :type=>5}, {:number=>7105, :type=>8}], {:number=>4465, :type=>3}=>[{:number=>6561, :type=>4}, {:number=>6501, :type=>5}, {:number=>6533, :type=>8}], {:number=>4656, :type=>3}=>[{:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>4753, :type=>3}=>[{:number=>5329, :type=>4}, {:number=>5356, :type=>6}], {:number=>4851, :type=>3}=>[{:number=>5184, :type=>4}, {:number=>5192, :type=>5}, {:number=>5151, :type=>6}], {:number=>5151, :type=>3}=>[{:number=>5184, :type=>4}, {:number=>5192, :type=>5}, {:number=>5151, :type=>6}], {:number=>5253, :type=>3}=>[{:number=>5329, :type=>4}, {:number=>5356, :type=>6}], {:number=>5356, :type=>3}=>[{:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>5565, :type=>3}=>[{:number=>6561, :type=>4}, {:number=>6501, :type=>5}, {:number=>6533, :type=>8}], {:number=>5671, :type=>3}=>[{:number=>7107, :type=>5}, {:number=>7105, :type=>8}], {:number=>5778, :type=>3}=>[{:number=>7875, :type=>6}], {:number=>5886, :type=>3}=>[{:number=>8649, :type=>4}, {:number=>8626, :type=>5}, {:number=>8646, :type=>6}, {:number=>8614, :type=>7}], {:number=>5995, :type=>3}=>[{:number=>9517, :type=>7}], {:number=>6105, :type=>3}=>[], {:number=>6216, :type=>3}=>[{:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>6328, :type=>3}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>6441, :type=>3}=>[{:number=>4187, :type=>5}, {:number=>4186, :type=>6}, {:number=>4141, :type=>7}], {:number=>6555, :type=>3}=>[{:number=>5551, :type=>5}, {:number=>5565, :type=>6}], {:number=>6786, :type=>3}=>[{:number=>8649, :type=>4}, {:number=>8626, :type=>5}, {:number=>8646, :type=>6}, {:number=>8614, :type=>7}], {:number=>6903, :type=>3}=>[], {:number=>7021, :type=>3}=>[{:number=>2116, :type=>4}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}, {:number=>2133, :type=>8}], {:number=>7381, :type=>3}=>[{:number=>8177, :type=>5}, {:number=>8128, :type=>6}], {:number=>7503, :type=>3}=>[], {:number=>7626, :type=>3}=>[{:number=>2601, :type=>4}, {:number=>2625, :type=>5}, {:number=>2673, :type=>7}], {:number=>7875, :type=>3}=>[{:number=>7569, :type=>4}, {:number=>7526, :type=>5}], {:number=>8001, :type=>3}=>[], {:number=>8128, :type=>3}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>8256, :type=>3}=>[{:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>8385, :type=>3}=>[], {:number=>8515, :type=>3}=>[{:number=>1521, :type=>4}, {:number=>1525, :type=>7}, {:number=>1541, :type=>8}], {:number=>8646, :type=>3}=>[{:number=>4624, :type=>4}, {:number=>4676, :type=>5}], {:number=>8778, :type=>3}=>[{:number=>7875, :type=>6}], {:number=>8911, :type=>3}=>[{:number=>1156, :type=>4}, {:number=>1162, :type=>5}, {:number=>1128, :type=>6}, {:number=>1177, :type=>7}], {:number=>9045, :type=>3}=>[{:number=>4558, :type=>7}], {:number=>9316, :type=>3}=>[{:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>9453, :type=>3}=>[{:number=>5329, :type=>4}, {:number=>5356, :type=>6}], {:number=>9591, :type=>3}=>[], {:number=>1024, :type=>4}=>[{:number=>2415, :type=>3}, {:number=>2485, :type=>3}, {:number=>2415, :type=>6}, {:number=>2465, :type=>8}], {:number=>1089, :type=>4}=>[{:number=>8911, :type=>3}, {:number=>8911, :type=>6}, {:number=>8965, :type=>8}], {:number=>1156, :type=>4}=>[{:number=>5671, :type=>3}, {:number=>5688, :type=>7}], {:number=>1225, :type=>4}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>1296, :type=>4}=>[{:number=>9633, :type=>8}], {:number=>1369, :type=>4}=>[{:number=>6903, :type=>3}, {:number=>6902, :type=>5}, {:number=>6903, :type=>6}, {:number=>6943, :type=>7}], {:number=>1444, :type=>4}=>[{:number=>4465, :type=>3}, {:number=>4485, :type=>8}], {:number=>1521, :type=>4}=>[{:number=>2145, :type=>3}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}, {:number=>2133, :type=>8}], {:number=>1681, :type=>4}=>[{:number=>8128, :type=>3}, {:number=>8177, :type=>5}, {:number=>8128, :type=>6}], {:number=>1764, :type=>4}=>[{:number=>6441, :type=>3}, {:number=>6441, :type=>6}, {:number=>6426, :type=>7}], {:number=>1849, :type=>4}=>[{:number=>4995, :type=>7}, {:number=>4961, :type=>8}], {:number=>1936, :type=>4}=>[{:number=>3655, :type=>3}, {:number=>3655, :type=>6}, {:number=>3605, :type=>8}], {:number=>2025, :type=>4}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>2116, :type=>4}=>[{:number=>1653, :type=>3}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>2209, :type=>4}=>[], {:number=>2304, :type=>4}=>[], {:number=>2401, :type=>4}=>[], {:number=>2601, :type=>4}=>[], {:number=>2704, :type=>4}=>[], {:number=>2809, :type=>4}=>[], {:number=>2916, :type=>4}=>[{:number=>1653, :type=>3}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>3025, :type=>4}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>3136, :type=>4}=>[{:number=>3655, :type=>3}, {:number=>3655, :type=>6}, {:number=>3605, :type=>8}], {:number=>3249, :type=>4}=>[{:number=>4995, :type=>7}, {:number=>4961, :type=>8}], {:number=>3364, :type=>4}=>[{:number=>6441, :type=>3}, {:number=>6441, :type=>6}, {:number=>6426, :type=>7}], {:number=>3481, :type=>4}=>[{:number=>8128, :type=>3}, {:number=>8177, :type=>5}, {:number=>8128, :type=>6}], {:number=>3721, :type=>4}=>[{:number=>2145, :type=>3}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}, {:number=>2133, :type=>8}], {:number=>3844, :type=>4}=>[{:number=>4465, :type=>3}, {:number=>4485, :type=>8}], {:number=>3969, :type=>4}=>[{:number=>6903, :type=>3}, {:number=>6902, :type=>5}, {:number=>6903, :type=>6}, {:number=>6943, :type=>7}], {:number=>4096, :type=>4}=>[{:number=>9633, :type=>8}], {:number=>4225, :type=>4}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>4356, :type=>4}=>[{:number=>5671, :type=>3}, {:number=>5688, :type=>7}], {:number=>4489, :type=>4}=>[{:number=>8911, :type=>3}, {:number=>8911, :type=>6}, {:number=>8965, :type=>8}], {:number=>4624, :type=>4}=>[{:number=>2415, :type=>3}, {:number=>2485, :type=>3}, {:number=>2415, :type=>6}, {:number=>2465, :type=>8}], {:number=>4761, :type=>4}=>[{:number=>6105, :type=>3}, {:number=>6112, :type=>5}, {:number=>6175, :type=>7}], {:number=>5041, :type=>4}=>[{:number=>4186, :type=>3}, {:number=>4187, :type=>5}, {:number=>4186, :type=>6}, {:number=>4141, :type=>7}], {:number=>5184, :type=>4}=>[], {:number=>5329, :type=>4}=>[{:number=>2926, :type=>3}], {:number=>5476, :type=>4}=>[{:number=>7626, :type=>3}, {:number=>7626, :type=>6}], {:number=>5625, :type=>4}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>5776, :type=>4}=>[{:number=>7626, :type=>3}, {:number=>7626, :type=>6}], {:number=>5929, :type=>4}=>[{:number=>2926, :type=>3}], {:number=>6084, :type=>4}=>[], {:number=>6241, :type=>4}=>[{:number=>4186, :type=>3}, {:number=>4187, :type=>5}, {:number=>4186, :type=>6}, {:number=>4141, :type=>7}], {:number=>6561, :type=>4}=>[{:number=>6105, :type=>3}, {:number=>6112, :type=>5}, {:number=>6175, :type=>7}], {:number=>6724, :type=>4}=>[{:number=>2415, :type=>3}, {:number=>2485, :type=>3}, {:number=>2415, :type=>6}, {:number=>2465, :type=>8}], {:number=>6889, :type=>4}=>[{:number=>8911, :type=>3}, {:number=>8911, :type=>6}, {:number=>8965, :type=>8}], {:number=>7056, :type=>4}=>[{:number=>5671, :type=>3}, {:number=>5688, :type=>7}], {:number=>7225, :type=>4}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>7396, :type=>4}=>[{:number=>9633, :type=>8}], {:number=>7569, :type=>4}=>[{:number=>6903, :type=>3}, {:number=>6902, :type=>5}, {:number=>6903, :type=>6}, {:number=>6943, :type=>7}], {:number=>7744, :type=>4}=>[{:number=>4465, :type=>3}, {:number=>4485, :type=>8}], {:number=>7921, :type=>4}=>[{:number=>2145, :type=>3}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}, {:number=>2133, :type=>8}], {:number=>8281, :type=>4}=>[{:number=>8128, :type=>3}, {:number=>8177, :type=>5}, {:number=>8128, :type=>6}], {:number=>8464, :type=>4}=>[{:number=>6441, :type=>3}, {:number=>6441, :type=>6}, {:number=>6426, :type=>7}], {:number=>8649, :type=>4}=>[{:number=>4995, :type=>7}, {:number=>4961, :type=>8}], {:number=>8836, :type=>4}=>[{:number=>3655, :type=>3}, {:number=>3655, :type=>6}, {:number=>3605, :type=>8}], {:number=>9025, :type=>4}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>9216, :type=>4}=>[{:number=>1653, :type=>3}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>9409, :type=>4}=>[], {:number=>9604, :type=>4}=>[], {:number=>9801, :type=>4}=>[], {:number=>1001, :type=>5}=>[], {:number=>1162, :type=>5}=>[{:number=>6216, :type=>3}, {:number=>6241, :type=>4}, {:number=>6216, :type=>6}, {:number=>6256, :type=>8}], {:number=>1247, :type=>5}=>[{:number=>4753, :type=>3}, {:number=>4761, :type=>4}, {:number=>4753, :type=>6}, {:number=>4774, :type=>7}], {:number=>1335, :type=>5}=>[{:number=>3553, :type=>7}], {:number=>1426, :type=>5}=>[{:number=>2628, :type=>3}, {:number=>2601, :type=>4}, {:number=>2673, :type=>7}], {:number=>1617, :type=>5}=>[{:number=>1711, :type=>3}, {:number=>1764, :type=>4}, {:number=>1782, :type=>7}], {:number=>1717, :type=>5}=>[{:number=>1711, :type=>3}, {:number=>1764, :type=>4}, {:number=>1782, :type=>7}], {:number=>1926, :type=>5}=>[{:number=>2628, :type=>3}, {:number=>2601, :type=>4}, {:number=>2673, :type=>7}], {:number=>2035, :type=>5}=>[{:number=>3553, :type=>7}], {:number=>2147, :type=>5}=>[{:number=>4753, :type=>3}, {:number=>4761, :type=>4}, {:number=>4753, :type=>6}, {:number=>4774, :type=>7}], {:number=>2262, :type=>5}=>[{:number=>6216, :type=>3}, {:number=>6241, :type=>4}, {:number=>6216, :type=>6}, {:number=>6256, :type=>8}], {:number=>2501, :type=>5}=>[], {:number=>2625, :type=>5}=>[{:number=>2556, :type=>3}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>2752, :type=>5}=>[{:number=>5253, :type=>3}, {:number=>5221, :type=>7}, {:number=>5208, :type=>8}], {:number=>2882, :type=>5}=>[{:number=>8256, :type=>3}, {:number=>8281, :type=>4}], {:number=>3015, :type=>5}=>[{:number=>1596, :type=>3}, {:number=>1521, :type=>4}, {:number=>1525, :type=>7}, {:number=>1541, :type=>8}], {:number=>3151, :type=>5}=>[{:number=>5151, :type=>3}, {:number=>5184, :type=>4}, {:number=>5151, :type=>6}], {:number=>3432, :type=>5}=>[{:number=>3249, :type=>4}, {:number=>3201, :type=>8}], {:number=>3577, :type=>5}=>[{:number=>7744, :type=>4}, {:number=>7756, :type=>7}, {:number=>7701, :type=>8}], {:number=>3725, :type=>5}=>[{:number=>2556, :type=>3}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>3876, :type=>5}=>[{:number=>7626, :type=>3}, {:number=>7626, :type=>6}], {:number=>4187, :type=>5}=>[{:number=>8778, :type=>3}], {:number=>4347, :type=>5}=>[{:number=>4753, :type=>3}, {:number=>4761, :type=>4}, {:number=>4753, :type=>6}, {:number=>4774, :type=>7}], {:number=>4676, :type=>5}=>[{:number=>7626, :type=>3}, {:number=>7626, :type=>6}], {:number=>4845, :type=>5}=>[{:number=>4558, :type=>7}], {:number=>5017, :type=>5}=>[{:number=>1711, :type=>3}, {:number=>1764, :type=>4}, {:number=>1782, :type=>7}], {:number=>5192, :type=>5}=>[{:number=>9216, :type=>4}, {:number=>9211, :type=>7}, {:number=>9296, :type=>8}], {:number=>5551, :type=>5}=>[{:number=>5151, :type=>3}, {:number=>5184, :type=>4}, {:number=>5151, :type=>6}], {:number=>5735, :type=>5}=>[{:number=>3553, :type=>7}], {:number=>5922, :type=>5}=>[{:number=>2211, :type=>3}, {:number=>2278, :type=>3}, {:number=>2209, :type=>4}, {:number=>2278, :type=>6}, {:number=>2205, :type=>7}, {:number=>2296, :type=>8}], {:number=>6112, :type=>5}=>[{:number=>1225, :type=>3}, {:number=>1275, :type=>3}, {:number=>1225, :type=>4}, {:number=>1296, :type=>4}, {:number=>1225, :type=>6}, {:number=>1288, :type=>7}, {:number=>1281, :type=>8}], {:number=>6305, :type=>5}=>[], {:number=>6501, :type=>5}=>[], {:number=>6902, :type=>5}=>[], {:number=>7107, :type=>5}=>[], {:number=>7315, :type=>5}=>[{:number=>1596, :type=>3}, {:number=>1521, :type=>4}, {:number=>1525, :type=>7}, {:number=>1541, :type=>8}], {:number=>7526, :type=>5}=>[{:number=>2628, :type=>3}, {:number=>2601, :type=>4}, {:number=>2673, :type=>7}], {:number=>7957, :type=>5}=>[{:number=>5778, :type=>3}, {:number=>5776, :type=>4}, {:number=>5778, :type=>6}], {:number=>8177, :type=>5}=>[{:number=>7744, :type=>4}, {:number=>7756, :type=>7}, {:number=>7701, :type=>8}], {:number=>8626, :type=>5}=>[{:number=>2628, :type=>3}, {:number=>2601, :type=>4}, {:number=>2673, :type=>7}], {:number=>8855, :type=>5}=>[{:number=>5565, :type=>3}, {:number=>5565, :type=>6}], {:number=>9087, :type=>5}=>[{:number=>8778, :type=>3}], {:number=>9322, :type=>5}=>[{:number=>2211, :type=>3}, {:number=>2278, :type=>3}, {:number=>2209, :type=>4}, {:number=>2278, :type=>6}, {:number=>2205, :type=>7}, {:number=>2296, :type=>8}], {:number=>9801, :type=>5}=>[], {:number=>1035, :type=>6}=>[{:number=>3577, :type=>5}, {:number=>3553, :type=>7}], {:number=>1128, :type=>6}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>1225, :type=>6}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2512, :type=>7}], {:number=>1326, :type=>6}=>[{:number=>2628, :type=>3}, {:number=>2601, :type=>4}, {:number=>2625, :type=>5}, {:number=>2673, :type=>7}], {:number=>1431, :type=>6}=>[{:number=>3136, :type=>4}, {:number=>3151, :type=>5}, {:number=>3186, :type=>7}], {:number=>1653, :type=>6}=>[{:number=>5356, :type=>3}, {:number=>5329, :type=>4}], {:number=>1891, :type=>6}=>[], {:number=>2016, :type=>6}=>[{:number=>1653, :type=>3}, {:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1651, :type=>7}], {:number=>2145, :type=>6}=>[{:number=>4558, :type=>7}], {:number=>2278, :type=>6}=>[{:number=>7875, :type=>3}], {:number=>2415, :type=>6}=>[{:number=>1596, :type=>3}, {:number=>1521, :type=>4}, {:number=>1525, :type=>7}, {:number=>1541, :type=>8}], {:number=>2556, :type=>6}=>[{:number=>5671, :type=>3}, {:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>2701, :type=>6}=>[], {:number=>3003, :type=>6}=>[], {:number=>3321, :type=>6}=>[{:number=>2145, :type=>3}, {:number=>2116, :type=>4}, {:number=>2147, :type=>5}, {:number=>2133, :type=>8}], {:number=>3486, :type=>6}=>[{:number=>8646, :type=>3}, {:number=>8649, :type=>4}, {:number=>8626, :type=>5}, {:number=>8614, :type=>7}], {:number=>3655, :type=>6}=>[{:number=>5565, :type=>3}, {:number=>5551, :type=>5}], {:number=>3828, :type=>6}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>4005, :type=>6}=>[], {:number=>4186, :type=>6}=>[{:number=>8646, :type=>3}, {:number=>8649, :type=>4}, {:number=>8626, :type=>5}, {:number=>8614, :type=>7}], {:number=>4371, :type=>6}=>[{:number=>7107, :type=>5}, {:number=>7105, :type=>8}], {:number=>4753, :type=>6}=>[{:number=>5356, :type=>3}, {:number=>5329, :type=>4}], {:number=>5151, :type=>6}=>[{:number=>5151, :type=>3}, {:number=>5184, :type=>4}, {:number=>5192, :type=>5}], {:number=>5356, :type=>6}=>[{:number=>5671, :type=>3}, {:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>5565, :type=>6}=>[{:number=>6555, :type=>3}, {:number=>6561, :type=>4}, {:number=>6501, :type=>5}, {:number=>6533, :type=>8}], {:number=>5778, :type=>6}=>[{:number=>7875, :type=>3}], {:number=>5995, :type=>6}=>[{:number=>9591, :type=>3}, {:number=>9517, :type=>7}], {:number=>6216, :type=>6}=>[{:number=>1653, :type=>3}, {:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1651, :type=>7}], {:number=>6441, :type=>6}=>[{:number=>4186, :type=>3}, {:number=>4187, :type=>5}, {:number=>4141, :type=>7}], {:number=>6903, :type=>6}=>[], {:number=>7381, :type=>6}=>[{:number=>8128, :type=>3}, {:number=>8177, :type=>5}], {:number=>7626, :type=>6}=>[{:number=>2628, :type=>3}, {:number=>2601, :type=>4}, {:number=>2625, :type=>5}, {:number=>2673, :type=>7}], {:number=>7875, :type=>6}=>[{:number=>7503, :type=>3}, {:number=>7569, :type=>4}, {:number=>7526, :type=>5}], {:number=>8128, :type=>6}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2839, :type=>7}, {:number=>2821, :type=>8}], {:number=>8385, :type=>6}=>[{:number=>8515, :type=>3}], {:number=>8646, :type=>6}=>[{:number=>4656, :type=>3}, {:number=>4624, :type=>4}, {:number=>4676, :type=>5}], {:number=>8911, :type=>6}=>[{:number=>1128, :type=>3}, {:number=>1176, :type=>3}, {:number=>1156, :type=>4}, {:number=>1162, :type=>5}, {:number=>1177, :type=>7}], {:number=>9453, :type=>6}=>[{:number=>5356, :type=>3}, {:number=>5329, :type=>4}], {:number=>1071, :type=>7}=>[{:number=>7107, :type=>5}, {:number=>7105, :type=>8}], {:number=>1177, :type=>7}=>[{:number=>7744, :type=>4}, {:number=>7701, :type=>8}], {:number=>1288, :type=>7}=>[{:number=>8836, :type=>4}, {:number=>8855, :type=>5}], {:number=>1404, :type=>7}=>[], {:number=>1525, :type=>7}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}], {:number=>1651, :type=>7}=>[{:number=>5151, :type=>3}, {:number=>5184, :type=>4}, {:number=>5192, :type=>5}, {:number=>5151, :type=>6}], {:number=>1782, :type=>7}=>[{:number=>8256, :type=>3}, {:number=>8281, :type=>4}], {:number=>1918, :type=>7}=>[{:number=>1891, :type=>3}, {:number=>1849, :type=>4}, {:number=>1891, :type=>6}, {:number=>1825, :type=>8}], {:number=>2059, :type=>7}=>[{:number=>5995, :type=>3}, {:number=>5929, :type=>4}, {:number=>5922, :type=>5}, {:number=>5995, :type=>6}, {:number=>5985, :type=>8}], {:number=>2205, :type=>7}=>[], {:number=>2356, :type=>7}=>[{:number=>5671, :type=>3}, {:number=>5625, :type=>4}], {:number=>2512, :type=>7}=>[{:number=>1225, :type=>3}, {:number=>1275, :type=>3}, {:number=>1225, :type=>4}, {:number=>1296, :type=>4}, {:number=>1247, :type=>5}, {:number=>1225, :type=>6}, {:number=>1281, :type=>8}], {:number=>2673, :type=>7}=>[{:number=>7381, :type=>3}, {:number=>7396, :type=>4}, {:number=>7315, :type=>5}, {:number=>7381, :type=>6}], {:number=>2839, :type=>7}=>[{:number=>3916, :type=>3}, {:number=>3969, :type=>4}], {:number=>3186, :type=>7}=>[{:number=>8646, :type=>3}, {:number=>8649, :type=>4}, {:number=>8626, :type=>5}, {:number=>8646, :type=>6}], {:number=>3367, :type=>7}=>[{:number=>6786, :type=>3}, {:number=>6724, :type=>4}], {:number=>3553, :type=>7}=>[{:number=>5356, :type=>3}, {:number=>5329, :type=>4}, {:number=>5356, :type=>6}], {:number=>3744, :type=>7}=>[{:number=>4465, :type=>3}, {:number=>4489, :type=>4}, {:number=>4485, :type=>8}], {:number=>4141, :type=>7}=>[{:number=>4186, :type=>3}, {:number=>4187, :type=>5}, {:number=>4186, :type=>6}], {:number=>4347, :type=>7}=>[{:number=>4753, :type=>3}, {:number=>4761, :type=>4}, {:number=>4753, :type=>6}], {:number=>4558, :type=>7}=>[{:number=>5886, :type=>3}], {:number=>4774, :type=>7}=>[], {:number=>4995, :type=>7}=>[{:number=>9591, :type=>3}], {:number=>5221, :type=>7}=>[{:number=>2145, :type=>3}, {:number=>2116, :type=>4}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}, {:number=>2133, :type=>8}], {:number=>5452, :type=>7}=>[{:number=>5253, :type=>3}, {:number=>5208, :type=>8}], {:number=>5688, :type=>7}=>[{:number=>8836, :type=>4}, {:number=>8855, :type=>5}], {:number=>5929, :type=>7}=>[{:number=>2926, :type=>3}, {:number=>2916, :type=>4}], {:number=>6175, :type=>7}=>[{:number=>7503, :type=>3}, {:number=>7569, :type=>4}, {:number=>7526, :type=>5}], {:number=>6426, :type=>7}=>[{:number=>2628, :type=>3}, {:number=>2601, :type=>4}, {:number=>2625, :type=>5}], {:number=>6682, :type=>7}=>[{:number=>8256, :type=>3}, {:number=>8281, :type=>4}], {:number=>6943, :type=>7}=>[{:number=>4371, :type=>3}, {:number=>4356, :type=>4}, {:number=>4347, :type=>5}, {:number=>4371, :type=>6}], {:number=>7209, :type=>7}=>[], {:number=>7756, :type=>7}=>[{:number=>5671, :type=>3}, {:number=>5625, :type=>4}], {:number=>8037, :type=>7}=>[{:number=>3741, :type=>3}, {:number=>3721, :type=>4}, {:number=>3725, :type=>5}], {:number=>8323, :type=>7}=>[{:number=>2346, :type=>3}, {:number=>2304, :type=>4}], {:number=>8614, :type=>7}=>[{:number=>1431, :type=>3}, {:number=>1485, :type=>3}, {:number=>1444, :type=>4}, {:number=>1426, :type=>5}, {:number=>1431, :type=>6}, {:number=>1408, :type=>8}], {:number=>9211, :type=>7}=>[{:number=>1128, :type=>3}, {:number=>1176, :type=>3}, {:number=>1156, :type=>4}, {:number=>1162, :type=>5}, {:number=>1128, :type=>6}], {:number=>9517, :type=>7}=>[{:number=>1711, :type=>3}, {:number=>1764, :type=>4}, {:number=>1717, :type=>5}], {:number=>9828, :type=>7}=>[{:number=>2809, :type=>4}, {:number=>2882, :type=>5}, {:number=>2821, :type=>8}], {:number=>1045, :type=>8}=>[{:number=>4558, :type=>7}], {:number=>1281, :type=>8}=>[{:number=>8128, :type=>3}, {:number=>8177, :type=>5}, {:number=>8128, :type=>6}], {:number=>1408, :type=>8}=>[], {:number=>1541, :type=>8}=>[{:number=>4186, :type=>3}, {:number=>4187, :type=>5}, {:number=>4186, :type=>6}, {:number=>4141, :type=>7}], {:number=>1825, :type=>8}=>[{:number=>2556, :type=>3}, {:number=>2501, :type=>5}, {:number=>2556, :type=>6}, {:number=>2512, :type=>7}], {:number=>1976, :type=>8}=>[{:number=>7626, :type=>3}, {:number=>7626, :type=>6}], {:number=>2133, :type=>8}=>[{:number=>3321, :type=>3}, {:number=>3364, :type=>4}, {:number=>3321, :type=>6}, {:number=>3367, :type=>7}], {:number=>2296, :type=>8}=>[{:number=>9604, :type=>4}], {:number=>2465, :type=>8}=>[{:number=>6555, :type=>3}, {:number=>6561, :type=>4}, {:number=>6501, :type=>5}], {:number=>2821, :type=>8}=>[{:number=>2145, :type=>3}, {:number=>2116, :type=>4}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}], {:number=>3008, :type=>8}=>[], {:number=>3201, :type=>8}=>[], {:number=>3605, :type=>8}=>[], {:number=>3816, :type=>8}=>[{:number=>1653, :type=>3}, {:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>4033, :type=>8}=>[{:number=>3321, :type=>3}, {:number=>3364, :type=>4}, {:number=>3321, :type=>6}, {:number=>3367, :type=>7}], {:number=>4256, :type=>8}=>[{:number=>5671, :type=>3}, {:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>4485, :type=>8}=>[{:number=>8515, :type=>3}], {:number=>4961, :type=>8}=>[{:number=>6105, :type=>3}, {:number=>6112, :type=>5}, {:number=>6175, :type=>7}], {:number=>5208, :type=>8}=>[], {:number=>5461, :type=>8}=>[{:number=>6105, :type=>3}, {:number=>6112, :type=>5}, {:number=>6175, :type=>7}], {:number=>5985, :type=>8}=>[{:number=>8515, :type=>3}], {:number=>6256, :type=>8}=>[{:number=>5671, :type=>3}, {:number=>5625, :type=>4}, {:number=>5688, :type=>7}], {:number=>6533, :type=>8}=>[{:number=>3321, :type=>3}, {:number=>3364, :type=>4}, {:number=>3321, :type=>6}, {:number=>3367, :type=>7}], {:number=>6816, :type=>8}=>[{:number=>1653, :type=>3}, {:number=>1681, :type=>4}, {:number=>1617, :type=>5}, {:number=>1653, :type=>6}, {:number=>1651, :type=>7}], {:number=>7105, :type=>8}=>[], {:number=>7701, :type=>8}=>[], {:number=>8008, :type=>8}=>[], {:number=>8321, :type=>8}=>[{:number=>2145, :type=>3}, {:number=>2116, :type=>4}, {:number=>2147, :type=>5}, {:number=>2145, :type=>6}], {:number=>8965, :type=>8}=>[{:number=>6555, :type=>3}, {:number=>6561, :type=>4}, {:number=>6501, :type=>5}], {:number=>9296, :type=>8}=>[{:number=>9604, :type=>4}], {:number=>9633, :type=>8}=>[{:number=>3321, :type=>3}, {:number=>3364, :type=>4}, {:number=>3321, :type=>6}, {:number=>3367, :type=>7}], {:number=>9976, :type=>8}=>[{:number=>7626, :type=>3}, {:number=>7626, :type=>6}]}"
142 ]
143 },
144 "execution_count": 8,
145 "metadata": {},
146 "output_type": "execute_result"
147 }
148 ],
149 "source": [
150 "connections = {}\n",
151 "triangles.each do |n|\n",
152 " connections[{number: n, type: 3}] = []\n",
153 "end\n",
154 "squares.each do |n|\n",
155 " connections[{number: n, type: 4}] = []\n",
156 "end\n",
157 "pentagonals.each do |n|\n",
158 " connections[{number: n, type: 5}] = []\n",
159 "end\n",
160 "hexagonals.each do |n|\n",
161 " connections[{number: n, type: 6}] = []\n",
162 "end\n",
163 "heptagonals.each do |n|\n",
164 " connections[{number: n, type: 7}] = []\n",
165 "end\n",
166 "octagonals.each do |n|\n",
167 " connections[{number: n, type: 8}] = []\n",
168 "end\n",
169 "\n",
170 "connections.keys.each do |nt| \n",
171 " connections.keys.select {|other| connects(nt[:number], other[:number]) && nt[:type] != other[:type]}.each do |other|\n",
172 " connections[nt] << other\n",
173 " end\n",
174 "end\n",
175 "\n",
176 "connections "
177 ]
178 },
179 {
180 "cell_type": "code",
181 "execution_count": 9,
182 "metadata": {},
183 "outputs": [
184 {
185 "data": {
186 "text/plain": [
187 ":initial_chains"
188 ]
189 },
190 "execution_count": 9,
191 "metadata": {},
192 "output_type": "execute_result"
193 }
194 ],
195 "source": [
196 "def initial_chains(connections)\n",
197 " connections.keys.select {|nt| nt[:type] == 8}.map {|nt| [nt]}\n",
198 "end"
199 ]
200 },
201 {
202 "cell_type": "code",
203 "execution_count": 10,
204 "metadata": {},
205 "outputs": [
206 {
207 "data": {
208 "text/plain": [
209 "[[{:number=>1045, :type=>8}], [{:number=>1281, :type=>8}], [{:number=>1408, :type=>8}], [{:number=>1541, :type=>8}], [{:number=>1825, :type=>8}], [{:number=>1976, :type=>8}], [{:number=>2133, :type=>8}], [{:number=>2296, :type=>8}], [{:number=>2465, :type=>8}], [{:number=>2821, :type=>8}], [{:number=>3008, :type=>8}], [{:number=>3201, :type=>8}], [{:number=>3605, :type=>8}], [{:number=>3816, :type=>8}], [{:number=>4033, :type=>8}], [{:number=>4256, :type=>8}], [{:number=>4485, :type=>8}], [{:number=>4961, :type=>8}], [{:number=>5208, :type=>8}], [{:number=>5461, :type=>8}], [{:number=>5985, :type=>8}], [{:number=>6256, :type=>8}], [{:number=>6533, :type=>8}], [{:number=>6816, :type=>8}], [{:number=>7105, :type=>8}], [{:number=>7701, :type=>8}], [{:number=>8008, :type=>8}], [{:number=>8321, :type=>8}], [{:number=>8965, :type=>8}], [{:number=>9296, :type=>8}], [{:number=>9633, :type=>8}], [{:number=>9976, :type=>8}]]"
210 ]
211 },
212 "execution_count": 10,
213 "metadata": {},
214 "output_type": "execute_result"
215 }
216 ],
217 "source": [
218 "initial_chains connections"
219 ]
220 },
221 {
222 "cell_type": "code",
223 "execution_count": 36,
224 "metadata": {},
225 "outputs": [
226 {
227 "data": {
228 "text/plain": [
229 ":can_add?"
230 ]
231 },
232 "execution_count": 36,
233 "metadata": {},
234 "output_type": "execute_result"
235 }
236 ],
237 "source": [
238 "def can_add?(chain, item, connections)\n",
239 " connections[chain.last].include?(item) &&\n",
240 " (! chain.map {|nt| nt[:type]}.include?(item[:type]))\n",
241 "end"
242 ]
243 },
244 {
245 "cell_type": "code",
246 "execution_count": 38,
247 "metadata": {},
248 "outputs": [
249 {
250 "data": {
251 "text/plain": [
252 "true"
253 ]
254 },
255 "execution_count": 38,
256 "metadata": {},
257 "output_type": "execute_result"
258 }
259 ],
260 "source": [
261 "connections[initial_chains(connections)[0].last].include?({:number=>4558, :type=>7})"
262 ]
263 },
264 {
265 "cell_type": "code",
266 "execution_count": 39,
267 "metadata": {},
268 "outputs": [
269 {
270 "data": {
271 "text/plain": [
272 "[8]"
273 ]
274 },
275 "execution_count": 39,
276 "metadata": {},
277 "output_type": "execute_result"
278 }
279 ],
280 "source": [
281 "initial_chains(connections)[0].map {|nt| nt[:type]}"
282 ]
283 },
284 {
285 "cell_type": "code",
286 "execution_count": 40,
287 "metadata": {},
288 "outputs": [
289 {
290 "data": {
291 "text/plain": [
292 ":extend_chain"
293 ]
294 },
295 "execution_count": 40,
296 "metadata": {},
297 "output_type": "execute_result"
298 }
299 ],
300 "source": [
301 "def extend_chain(chain, connections)\n",
302 " connections[chain.last].select {|e| can_add? chain, e, connections}.map {|e| chain + [e]}\n",
303 "end"
304 ]
305 },
306 {
307 "cell_type": "code",
308 "execution_count": 43,
309 "metadata": {},
310 "outputs": [
311 {
312 "data": {
313 "text/plain": [
314 "[[{:number=>1281, :type=>8}, {:number=>8128, :type=>3}], [{:number=>1281, :type=>8}, {:number=>8177, :type=>5}], [{:number=>1281, :type=>8}, {:number=>8128, :type=>6}]]"
315 ]
316 },
317 "execution_count": 43,
318 "metadata": {},
319 "output_type": "execute_result"
320 }
321 ],
322 "source": [
323 "extend_chain(initial_chains(connections)[1], connections)"
324 ]
325 },
326 {
327 "cell_type": "code",
328 "execution_count": 53,
329 "metadata": {},
330 "outputs": [
331 {
332 "data": {
333 "text/plain": [
334 ":complete?"
335 ]
336 },
337 "execution_count": 53,
338 "metadata": {},
339 "output_type": "execute_result"
340 }
341 ],
342 "source": [
343 "def complete?(chain, connections)\n",
344 " chain.length == 6 && Set.new(chain.map {|nt| nt[:type]}).length == 6 && connections[chain.last].include?(chain.first)\n",
345 "end"
346 ]
347 },
348 {
349 "cell_type": "code",
350 "execution_count": 56,
351 "metadata": {
352 "scrolled": true
353 },
354 "outputs": [
355 {
356 "data": {
357 "text/plain": [
358 "28684"
359 ]
360 },
361 "execution_count": 56,
362 "metadata": {},
363 "output_type": "execute_result"
364 }
365 ],
366 "source": [
367 "agenda = initial_chains(connections)\n",
368 "while ! complete?(agenda.first, connections)\n",
369 " # puts agenda.first, agenda.length\n",
370 " new_chains = extend_chain(agenda.first, connections)\n",
371 " agenda = agenda.drop(1) + new_chains\n",
372 "end\n",
373 "agenda.first.map{|nt| nt[:number]}.sum"
374 ]
375 },
376 {
377 "cell_type": "code",
378 "execution_count": 58,
379 "metadata": {},
380 "outputs": [
381 {
382 "data": {
383 "text/plain": [
384 "224"
385 ]
386 },
387 "execution_count": 58,
388 "metadata": {},
389 "output_type": "execute_result"
390 }
391 ],
392 "source": [
393 "agenda.length"
394 ]
395 },
396 {
397 "cell_type": "code",
398 "execution_count": null,
399 "metadata": {
400 "collapsed": true
401 },
402 "outputs": [],
403 "source": []
404 }
405 ],
406 "metadata": {
407 "kernelspec": {
408 "display_name": "Ruby 2.4.0",
409 "language": "ruby",
410 "name": "ruby"
411 },
412 "language_info": {
413 "file_extension": ".rb",
414 "mimetype": "application/x-ruby",
415 "name": "ruby",
416 "version": "2.4.0"
417 }
418 },
419 "nbformat": 4,
420 "nbformat_minor": 2
421 }