+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
5e96e12
master
1state = set () 2with open ("test" )as f : 3key = [1 if c == "#" else 0 for c in f .readline ()] 4f .readline () 5for y ,l in enumerate (f ): 6for x ,c in enumerate (l ): 7if c == "#" : 8state .add ((x ,y )) 9 10symbols = ("." ,"#" ) 11nums = ("0" ,"1" ) 12neighbors = lambda x ,y : [(x + a ,y + b )for b in range (- 1 ,2 )for a in range (- 1 ,2 )] 13lookup = lambda x ,y :key [int ("" .join (nums [n in state ]for n in neighbors (x ,y )),2 )] 14 15 16def bounding_box (points ): 17xs ,ys = zip (* points ) 18return (min (xs ),min (ys ),max (xs ),max (ys )) 19 20 21def step (inv = False ): 22new = set () 23l ,t ,r ,b = bounding_box (state ) 24for y in range (t - 1 ,b + 2 ): 25for x in range (l - 1 ,r + 2 ): 26if inv != lookup (x ,y ): 27new .add ((x ,y )) 28return new 29 30 31def display (inv = False ): 32l ,t ,r ,b = bounding_box (state ) 33for y in range (t - 1 ,b + 2 ): 34for x in range (l - 1 ,r + 2 ): 35symbols [inv != ((x ,y )in state )],end = "" ) 36# display() 40for i in range (2 ): 41state = step (i % 2 == 0 ) 42len (state )) 43# display(i % 2 == 0)