+/AoC

advent of code

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

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

master
1.8 KiB61 linesraw
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):
14    for x in range(width):
15        if (c := lines[y + 1][x + 1]) != ".":
16            bliz[arrows.index(c)].append((x, y))
17
18
19def show(bliz, touched):
20    for y in range(-1, height + 1):
21        for x in range(-1, width + 1):
22            c = "." if (x, y) in spaces else "#"
23            for i in range(4):
24                if (x, y) in bliz[i]:
25                    if c in arrows:
26                        c = "2"
27                    elif c == "2":
28                        c = "3"
29                    else:
30                        c = arrows[i]
31            if (x, y) in touched:
32                c = "E"
33            print(c, end="")
34        print()
35    print()
36
37
38def search(cur, to, time):
39    touched = {cur}
40    # show(bliz, touched)
41    for time in count(time + 1):
42        for i in range(4):
43            dx, dy = dirs[i]
44            for j in range(len(bliz[i])):
45                x, y = bliz[i][j]
46                bliz[i][j] = ((x + dx + width) % width, (y + dy + height) % height)
47
48        new = set()
49        for pos in touched:
50            new |= {tuple(map(add, pos, d)) for d in dirs} & spaces
51        new -= reduce(or_, map(set, bliz))
52
53        touched = new
54        # show(bliz, touched)
55        if to in touched:
56            return time
57
58
59print(there := search(start, end, 0))
60back = search(end, start, there)
61print(search(start, end, back))