+/AoC
advent of code
git clone https://git.julia.blue/+/AoC
0579d0a
master
1import re 2from operator import add ,sub 3 4board ,steps = open ("small" ).read ().split ("\n\n" ) 5spaces = {} 6cur = None 7for r ,row in enumerate (board .splitlines ()): 8for c ,char in enumerate (row ): 9if char != " " : 10spaces [(r + 1 ,c + 1 )]= char 11if not cur : 12cur = (r + 1 ,c + 1 ) 13 14 15def move (pos ,dir ,times ): 16diff = ((0 ,1 ), (1 ,0 ), (0 ,- 1 ), (- 1 ,0 ))[dir ] 17for _ in range (times ): 18new = tuple (map (add ,pos ,diff )) 19if not (s := spaces .get (new )): 20while True : 21new = tuple (map (sub ,new ,diff )) 22if not (s := spaces .get (new )): 23new = tuple (map (add ,new ,diff )) 24s = spaces .get (new ) 25break 26# print(" ", new, s) 27if s == "#" : 28return pos 29pos = new 30return pos 31 32 33num = [int (s )for s in re .split ("R|L" ,steps )] 34let = [(s == "R" )* 2 - 1 for s in re .split ("\d+" ,steps .strip ())if s ]+ [0 ] 35dir = 0 36for n ,turn in zip (num ,let ): 37# print(cur, dir, n, turn) 38cur = move (cur ,dir ,n ) 39dir = (dir + 4 + turn )% 4 40# print(cur, dir, n, turn) 41 421000 * cur [0 ]+ 4 * cur [1 ]+ dir )