+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
38c0746
master
1from math import sqrt 2 3n = 347991 4 5ring = 0 6while 4 * (ring ** 2 + ring )< n : 7ring += 1 8ring -= 1 9temp = n - 4 * (ring ** 2 + ring )- 1 10 11"Part 1:" ,ring * 2 + sum (([1 ]+ [- 1 ]* ring + ([1 ]* (ring + 1 )+ [- 1 ]* (ring + 1 ))* 3 + [1 ]* (ring + 1 ))[:temp ]) 12 13import numpy as np 14 15rotate = lambda x :np .asarray (np .matrix ([x [0 ],x [1 ]])* np .matrix ([[0 ,1 ], [- 1 ,0 ]]))[0 ] 16 17board = np .zeros ((300 ,300 ),np .int32 ) 18pos = np .array ([150 ,150 ]) 19direction = np .array ([1 ,0 ]) 20neighbors = [(i ,j )for i in range (- 1 ,2 )for j in range (- 1 ,2 )] 21board [pos [0 ],pos [1 ]]= 1 22pos += direction 23 24for i in range (n ): 25board [pos [0 ],pos [1 ]]= sum (board [pos [0 ]+ i [0 ],pos [1 ]+ i [1 ]]for i in neighbors ) 26if board [pos [0 ],pos [1 ]]> n : 27"Part 2:" ,board [pos [0 ],pos [1 ]] 28break 29temp = rotate (direction ) 30if not board [pos [0 ]+ temp [0 ],pos [1 ]+ temp [1 ]]: 31direction = rotate (direction ) 32pos += direction