--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "require_relative 'array-numbers'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":valid_set"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def valid_set(&block)\n",
+ " (1..150).map {|i| block.call i}.select {|i| i >= 1000 && i <= 10000 && i % 10 != 0}\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[[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]]"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "triangles = valid_set {|n| n * (n + 1) / 2}\n",
+ "pentagonals = valid_set {|n| n * (3 * n - 1) / 2}\n",
+ "heptagonals = valid_set {|n| n * (5 * n - 3) / 2}\n",
+ "squares = valid_set {|n| n * n}\n",
+ "hexagonals = valid_set {|n| n * (2 * n - 1)}\n",
+ "octagonals = valid_set {|n| n * (3 * n - 2)}\n",
+ "[triangles, pentagonals, heptagonals, squares, hexagonals, octagonals]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":connects"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def connects(n1, n2)\n",
+ " n1.to_digits[2..3] == n2.to_digits[0..1]\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "connects(1234, 3456)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "false"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "connects(1234, 1234)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "{{: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}]}"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "connections = {}\n",
+ "triangles.each do |n|\n",
+ " connections[{number: n, type: 3}] = []\n",
+ "end\n",
+ "squares.each do |n|\n",
+ " connections[{number: n, type: 4}] = []\n",
+ "end\n",
+ "pentagonals.each do |n|\n",
+ " connections[{number: n, type: 5}] = []\n",
+ "end\n",
+ "hexagonals.each do |n|\n",
+ " connections[{number: n, type: 6}] = []\n",
+ "end\n",
+ "heptagonals.each do |n|\n",
+ " connections[{number: n, type: 7}] = []\n",
+ "end\n",
+ "octagonals.each do |n|\n",
+ " connections[{number: n, type: 8}] = []\n",
+ "end\n",
+ "\n",
+ "connections.keys.each do |nt| \n",
+ " connections.keys.select {|other| connects(nt[:number], other[:number]) && nt[:type] != other[:type]}.each do |other|\n",
+ " connections[nt] << other\n",
+ " end\n",
+ "end\n",
+ "\n",
+ "connections "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":initial_chains"
+ ]
+ },
+ "execution_count": 9,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def initial_chains(connections)\n",
+ " connections.keys.select {|nt| nt[:type] == 8}.map {|nt| [nt]}\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[[{: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}]]"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "initial_chains connections"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":can_add?"
+ ]
+ },
+ "execution_count": 36,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def can_add?(chain, item, connections)\n",
+ " connections[chain.last].include?(item) &&\n",
+ " (! chain.map {|nt| nt[:type]}.include?(item[:type]))\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 38,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "true"
+ ]
+ },
+ "execution_count": 38,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "connections[initial_chains(connections)[0].last].include?({:number=>4558, :type=>7})"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 39,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[8]"
+ ]
+ },
+ "execution_count": 39,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "initial_chains(connections)[0].map {|nt| nt[:type]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 40,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":extend_chain"
+ ]
+ },
+ "execution_count": 40,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def extend_chain(chain, connections)\n",
+ " connections[chain.last].select {|e| can_add? chain, e, connections}.map {|e| chain + [e]}\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 43,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[[{:number=>1281, :type=>8}, {:number=>8128, :type=>3}], [{:number=>1281, :type=>8}, {:number=>8177, :type=>5}], [{:number=>1281, :type=>8}, {:number=>8128, :type=>6}]]"
+ ]
+ },
+ "execution_count": 43,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "extend_chain(initial_chains(connections)[1], connections)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 53,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":complete?"
+ ]
+ },
+ "execution_count": 53,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def complete?(chain, connections)\n",
+ " chain.length == 6 && Set.new(chain.map {|nt| nt[:type]}).length == 6 && connections[chain.last].include?(chain.first)\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 56,
+ "metadata": {
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "28684"
+ ]
+ },
+ "execution_count": 56,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "agenda = initial_chains(connections)\n",
+ "while ! complete?(agenda.first, connections)\n",
+ " # puts agenda.first, agenda.length\n",
+ " new_chains = extend_chain(agenda.first, connections)\n",
+ " agenda = agenda.drop(1) + new_chains\n",
+ "end\n",
+ "agenda.first.map{|nt| nt[:number]}.sum"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 58,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "224"
+ ]
+ },
+ "execution_count": 58,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "agenda.length"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Ruby 2.4.0",
+ "language": "ruby",
+ "name": "ruby"
+ },
+ "language_info": {
+ "file_extension": ".rb",
+ "mimetype": "application/x-ruby",
+ "name": "ruby",
+ "version": "2.4.0"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}