+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
0579d0a
master
1from itertools import product ,count 2from functools import reduce 3from operator import or_ ,add 4 5lines = open ("input" ).readlines () 6width ,height = len (lines [0 ])- 3 ,len (lines )- 2 7start = (lines [0 ].index ("." )- 1 ,- 1 ) 8end = (lines [- 1 ].index ("." )- 1 ,height ) 9spaces = set (product (range (width ),range (height )))| {start ,end } 10dirs = ((1 ,0 ), (- 1 ,0 ), (0 ,1 ), (0 ,- 1 ), (0 ,0 )) 11arrows = "><v^" 12bliz = [[]for i in range (4 )] 13for y in range (height ): 14for x in range (width ): 15if (c := lines [y + 1 ][x + 1 ])!= "." : 16bliz [arrows .index (c )].append ((x ,y )) 17 18 19def show (bliz ,touched ): 20for y in range (- 1 ,height + 1 ): 21for x in range (- 1 ,width + 1 ): 22c = "." if (x ,y )in spaces else "#" 23for i in range (4 ): 24if (x ,y )in bliz [i ]: 25if c in arrows : 26c = "2" 27elif c == "2" : 28c = "3" 29else : 30c = arrows [i ] 31if (x ,y )in touched : 32c = "E" 33c ,end = "" ) 34def search (cur ,to ,time ): 39touched = {cur } 40# show(bliz, touched) 41for time in count (time + 1 ): 42for i in range (4 ): 43dx ,dy = dirs [i ] 44for j in range (len (bliz [i ])): 45x ,y = bliz [i ][j ] 46bliz [i ][j ]= ((x + dx + width )% width , (y + dy + height )% height ) 47 48new = set () 49for pos in touched : 50new |= {tuple (map (add ,pos ,d ))for d in dirs }& spaces 51new -= reduce (or_ ,map (set ,bliz )) 52 53touched = new 54# show(bliz, touched) 55if to in touched : 56return time 57 58 59there := search (start ,end ,0 )) 60back = search (end ,start ,there ) 61search (start ,end ,back ))