2017-12-03 - Spiral Memory
(original .ipynb)from pprint import pprint my_puzzle_input = 289326 class Spiral(object): def __init__(self, first, step): self.first = first + 1 self.last = None self.step = step def __repr__(self): return str(self.first) + " - " + str(self.last) i = 1 step = 2 side = 0 all_corners = [] all_spirals = [] corners = [] current_spiral = Spiral(i, step) done = False while not done: if side > 3: side = 0 step += 2 current_spiral.last = i all_spirals.append(current_spiral) all_corners.append(corners) corners = [] current_spiral = Spiral(i, step) if i >= my_puzzle_input: done = True i += step side += 1 corners.append(i) #pprint(all_corners) print(dir(current_spiral)) pprint(all_spirals)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'first', 'last', 'step'] [2 - 9, 10 - 25, 26 - 49, 50 - 81, 82 - 121, 122 - 169, 170 - 225, 226 - 289, 290 - 361, 362 - 441, 442 - 529, 530 - 625, 626 - 729, 730 - 841, 842 - 961, 962 - 1089, 1090 - 1225, 1226 - 1369, 1370 - 1521, 1522 - 1681, 1682 - 1849, 1850 - 2025, 2026 - 2209, 2210 - 2401, 2402 - 2601, 2602 - 2809, 2810 - 3025, 3026 - 3249, 3250 - 3481, 3482 - 3721, 3722 - 3969, 3970 - 4225, 4226 - 4489, 4490 - 4761, 4762 - 5041, 5042 - 5329, 5330 - 5625, 5626 - 5929, 5930 - 6241, 6242 - 6561, 6562 - 6889, 6890 - 7225, 7226 - 7569, 7570 - 7921, 7922 - 8281, 8282 - 8649, 8650 - 9025, 9026 - 9409, 9410 - 9801, 9802 - 10201, 10202 - 10609, 10610 - 11025, 11026 - 11449, 11450 - 11881, 11882 - 12321, 12322 - 12769, 12770 - 13225, 13226 - 13689, 13690 - 14161, 14162 - 14641, 14642 - 15129, 15130 - 15625, 15626 - 16129, 16130 - 16641, 16642 - 17161, 17162 - 17689, 17690 - 18225, 18226 - 18769, 18770 - 19321, 19322 - 19881, 19882 - 20449, 20450 - 21025, 21026 - 21609, 21610 - 22201, 22202 - 22801, 22802 - 23409, 23410 - 24025, 24026 - 24649, 24650 - 25281, 25282 - 25921, 25922 - 26569, 26570 - 27225, 27226 - 27889, 27890 - 28561, 28562 - 29241, 29242 - 29929, 29930 - 30625, 30626 - 31329, 31330 - 32041, 32042 - 32761, 32762 - 33489, 33490 - 34225, 34226 - 34969, 34970 - 35721, 35722 - 36481, 36482 - 37249, 37250 - 38025, 38026 - 38809, 38810 - 39601, 39602 - 40401, 40402 - 41209, 41210 - 42025, 42026 - 42849, 42850 - 43681, 43682 - 44521, 44522 - 45369, 45370 - 46225, 46226 - 47089, 47090 - 47961, 47962 - 48841, 48842 - 49729, 49730 - 50625, 50626 - 51529, 51530 - 52441, 52442 - 53361, 53362 - 54289, 54290 - 55225, 55226 - 56169, 56170 - 57121, 57122 - 58081, 58082 - 59049, 59050 - 60025, 60026 - 61009, 61010 - 62001, 62002 - 63001, 63002 - 64009, 64010 - 65025, 65026 - 66049, 66050 - 67081, 67082 - 68121, 68122 - 69169, 69170 - 70225, 70226 - 71289, 71290 - 72361, 72362 - 73441, 73442 - 74529, 74530 - 75625, 75626 - 76729, 76730 - 77841, 77842 - 78961, 78962 - 80089, 80090 - 81225, 81226 - 82369, 82370 - 83521, 83522 - 84681, 84682 - 85849, 85850 - 87025, 87026 - 88209, 88210 - 89401, 89402 - 90601, 90602 - 91809, 91810 - 93025, 93026 - 94249, 94250 - 95481, 95482 - 96721, 96722 - 97969, 97970 - 99225, 99226 - 100489, 100490 - 101761, 101762 - 103041, 103042 - 104329, 104330 - 105625, 105626 - 106929, 106930 - 108241, 108242 - 109561, 109562 - 110889, 110890 - 112225, 112226 - 113569, 113570 - 114921, 114922 - 116281, 116282 - 117649, 117650 - 119025, 119026 - 120409, 120410 - 121801, 121802 - 123201, 123202 - 124609, 124610 - 126025, 126026 - 127449, 127450 - 128881, 128882 - 130321, 130322 - 131769, 131770 - 133225, 133226 - 134689, 134690 - 136161, 136162 - 137641, 137642 - 139129, 139130 - 140625, 140626 - 142129, 142130 - 143641, 143642 - 145161, 145162 - 146689, 146690 - 148225, 148226 - 149769, 149770 - 151321, 151322 - 152881, 152882 - 154449, 154450 - 156025, 156026 - 157609, 157610 - 159201, 159202 - 160801, 160802 - 162409, 162410 - 164025, 164026 - 165649, 165650 - 167281, 167282 - 168921, 168922 - 170569, 170570 - 172225, 172226 - 173889, 173890 - 175561, 175562 - 177241, 177242 - 178929, 178930 - 180625, 180626 - 182329, 182330 - 184041, 184042 - 185761, 185762 - 187489, 187490 - 189225, 189226 - 190969, 190970 - 192721, 192722 - 194481, 194482 - 196249, 196250 - 198025, 198026 - 199809, 199810 - 201601, 201602 - 203401, 203402 - 205209, 205210 - 207025, 207026 - 208849, 208850 - 210681, 210682 - 212521, 212522 - 214369, 214370 - 216225, 216226 - 218089, 218090 - 219961, 219962 - 221841, 221842 - 223729, 223730 - 225625, 225626 - 227529, 227530 - 229441, 229442 - 231361, 231362 - 233289, 233290 - 235225, 235226 - 237169, 237170 - 239121, 239122 - 241081, 241082 - 243049, 243050 - 245025, 245026 - 247009, 247010 - 249001, 249002 - 251001, 251002 - 253009, 253010 - 255025, 255026 - 257049, 257050 - 259081, 259082 - 261121, 261122 - 263169, 263170 - 265225, 265226 - 267289, 267290 - 269361, 269362 - 271441, 271442 - 273529, 273530 - 275625, 275626 - 277729, 277730 - 279841, 279842 - 281961, 281962 - 284089, 284090 - 286225, 286226 - 288369, 288370 - 290521]
len(all_spirals)
269
Part 2
As a stress test on the system, the programs here clear the grid and then store the value 1 in square 1. Then, in the same allocation order as shown above, they store the sum of the values in all adjacent squares, including diagonals.
So, the first few squares' values are chosen as follows:
- Square 1 starts with the value 1.
- Square 2 has only one adjacent filled square (with value 1), so it also stores 1.
- Square 3 has both of the above squares as neighbors and stores the sum of their values, 2.
- Square 4 has all three of the aforementioned squares as neighbors and stores the sum of their values, 4.
- Square 5 only has the first and fourth squares as neighbors, so it gets the value 5.
Once a square is written, its value does not change. Therefore, the first few squares would receive the following values:
147 142 133 122 59
304 5 4 2 57
330 10 1 1 54
351 11 23 25 26
362 747 806---> ...
What is the first value written that is larger than your puzzle input?
def grow(indices): return indices + [indices[-1]+1] def proceed(indices): return indices[1:] + [indices[-1]+1] def shrink(indices): return indices[1:] def create_inner_indexer(length): yield grow while True: if length > 16: for n in range((length // 4) -4): yield proceed yield shrink yield shrink yield grow yield grow def find_inner_connecting_indices(length): if length == 8: return [[0]] * 8 indexer_generator = create_inner_indexer(length) indices = [length - 8 - 1, 0] all_indices = [] while length > 0: all_indices.append(indices) indexer = next(indexer_generator) indices = indexer(indices) length -= 1 return all_indices from pprint import pprint #pprint(find_inner_connecting_indices(8)) #pprint(find_inner_connecting_indices(16)) pprint(find_inner_connecting_indices(32))
[[23, 0], [23, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5], [5], [5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10], [9, 10, 11], [10, 11], [11], [11, 12], [11, 12, 13], [12, 13, 14], [13, 14, 15], [14, 15, 16], [15, 16, 17], [16, 17], [17], [17, 18], [17, 18, 19], [18, 19, 20], [19, 20, 21], [20, 21, 22], [21, 22, 23], [22, 23], [23]]
def create_current_indexer(square_length): yield [None] n = 0 while True: if n > 0 and (n+1) % (square_length/4) == 0: yield [n-1, n] else: yield [n] n += 1 def find_current_connecting_indices(square_length): indexer = create_current_indexer(square_length) all_indices = [ next(indexer) for _ in range(square_length) ] all_indices[-2].append(0) all_indices[-1].append(0) return all_indices print(find_current_connecting_indices(8))
[[None], [0], [0, 1], [2], [2, 3], [4], [4, 5, 0], [6, 0]]
from pprint import pprint def solve(threshold): inner_circle = [1] current_circle = [] current_len = 8 stop = False while not stop: indices = zip( find_inner_connecting_indices(current_len), find_current_connecting_indices(current_len) ) for inner, current in indices: print(inner) print(current) n = sum([ inner_circle[i] for i in inner]) + sum([ current_circle[i] for i in current if i != None]) print(n) if (n > threshold): print("FOUND IT", n) stop = True return #print("=>", n, "\n") current_circle.append(n) print("=>", inner_circle) print("=>", current_circle, "\n") #pprint(current_circle) inner_circle = current_circle current_circle = [] current_len += 8 return -1 solve(289326)
[0] [None] 1 => [1] => [1] [0] [0] 2 => [1] => [1, 2] [0] [0, 1] 4 => [1] => [1, 2, 4] [0] [2] 5 => [1] => [1, 2, 4, 5] [0] [2, 3] 10 => [1] => [1, 2, 4, 5, 10] [0] [4] 11 => [1] => [1, 2, 4, 5, 10, 11] [0] [4, 5, 0] 23 => [1] => [1, 2, 4, 5, 10, 11, 23] [0] [6, 0] 25 => [1] => [1, 2, 4, 5, 10, 11, 23, 25] [7, 0] [None] 26 => [1, 2, 4, 5, 10, 11, 23, 25] => [26] [7, 0, 1] [0] 54 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54] [0, 1] [1] 57 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57] [1] [2] 59 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59] [1, 2] [2, 3] 122 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122] [1, 2, 3] [4] 133 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133] [2, 3] [5] 142 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142] [3] [6] 147 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147] [3, 4] [6, 7] 304 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304] [3, 4, 5] [8] 330 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330] [4, 5] [9] 351 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351] [5] [10] 362 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362] [5, 6] [10, 11] 747 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747] [5, 6, 7] [12] 806 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806] [6, 7] [13, 0] 880 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880] [7] [14, 0] 931 => [1, 2, 4, 5, 10, 11, 23, 25] => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] [15, 0] [None] 957 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957] [15, 0, 1] [0] 1968 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968] [0, 1, 2] [1] 2105 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105] [1, 2, 3] [2] 2275 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275] [2, 3] [3] 2391 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391] [3] [4] 2450 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450] [3, 4] [4, 5] 5022 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022] [3, 4, 5] [6] 5336 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336] [4, 5, 6] [7] 5733 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733] [5, 6, 7] [8] 6155 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155] [6, 7] [9] 6444 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444] [7] [10] 6591 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591] [7, 8] [10, 11] 13486 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486] [7, 8, 9] [12] 14267 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267] [8, 9, 10] [13] 15252 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252] [9, 10, 11] [14] 16295 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295] [10, 11] [15] 17008 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008] [11] [16] 17370 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370] [11, 12] [16, 17] 35487 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487] [11, 12, 13] [18] 37402 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402] [12, 13, 14] [19] 39835 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835] [13, 14, 15] [20] 42452 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452] [14, 15] [21, 0] 45220 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220] [15] [22, 0] 47108 => [26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806, 880, 931] => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] [23, 0] [None] 48065 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065] [23, 0, 1] [0] 98098 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098] [0, 1, 2] [1] 103128 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128] [1, 2, 3] [2] 109476 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128, 109476] [2, 3, 4] [3] 116247 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128, 109476, 116247] [3, 4, 5] [4] 123363 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128, 109476, 116247, 123363] [4, 5] [5] 128204 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128, 109476, 116247, 123363, 128204] [5] [6] 130654 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128, 109476, 116247, 123363, 128204, 130654] [5, 6] [6, 7] 266330 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128, 109476, 116247, 123363, 128204, 130654, 266330] [5, 6, 7] [8] 279138 => [957, 1968, 2105, 2275, 2391, 2450, 5022, 5336, 5733, 6155, 6444, 6591, 13486, 14267, 15252, 16295, 17008, 17370, 35487, 37402, 39835, 42452, 45220, 47108] => [48065, 98098, 103128, 109476, 116247, 123363, 128204, 130654, 266330, 279138] [6, 7, 8] [9] 295229 FOUND IT 295229