+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanA lot closer this year! (missing 19 and 22b)0579d0a

master
1.1 KiB42 linesraw
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()):
8    for c, char in enumerate(row):
9        if char != " ":
10            spaces[(r + 1, c + 1)] = char
11            if not cur:
12                cur = (r + 1, c + 1)
13
14
15def move(pos, dir, times):
16    diff = ((0, 1), (1, 0), (0, -1), (-1, 0))[dir]
17    for _ in range(times):
18        new = tuple(map(add, pos, diff))
19        if not (s := spaces.get(new)):
20            while True:
21                new = tuple(map(sub, new, diff))
22                if not (s := spaces.get(new)):
23                    new = tuple(map(add, new, diff))
24                    s = spaces.get(new)
25                    break
26        # print(" ", new, s)
27        if s == "#":
28            return pos
29        pos = new
30    return 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)
38    cur = move(cur, dir, n)
39    dir = (dir + 4 + turn) % 4
40# print(cur, dir, n, turn)
41
42print(1000 * cur[0] + 4 * cur[1] + dir)