+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
d3d9f14
master
1import re 2from itertools import cycle 3 4with open ("small2" )as f : 5directions = f .readline ().strip () 6f .readline () 7paths = {} 8for line in f : 9name ,l ,r = re .findall (r"(...) = \((...), (...)\)" ,line )[0 ] 10paths [name ]= (l ,r ) 11 12cur = "AAA" 13for i ,c in enumerate (cycle (directions ),start = 1 ): 14cur = paths [cur ][c == "R" ] 15if cur == "ZZZ" : 16i ) 17break 18 19starts = [s for s in paths .keys ()if s .endswith ("A" )] 20found = [[]for _ in range (len (starts ))] 21 22for s in starts : 23t = (- 1 ,"AAA" ) 24h = (0 ,paths ["AAA" ][directions [0 ]== "R" ]) 25while t != h : 26i ,t = t 27j ,h = h 28i = i + 1 % len (directions ) 29t = (i ,paths [t ][directions [i ]== "R" ]) 30t = (i + 1 ,paths [t ][directions [i + 1 ]== "R" ]) 31t ,h ) 32 33# for i in cycle(range(len(directions))) 34# for s in range(len(starts)): 35# starts[s] = paths[starts[s]][c == "R"] 36# if starts[s].endswith("Z"): 37# found[s].append(i % len(directions)) 38# if i > 10000: 39# break 40found ) 41starts )