+/AoC

advent of code

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

Joseph RyanDay 8 (part 1)d3d9f14

master
1.0 KiB41 linesraw
1import re
2from itertools import cycle
3
4with open("small2") as f:
5    directions = f.readline().strip()
6    f.readline()
7    paths = {}
8    for line in f:
9        name, l, r = re.findall(r"(...) = \((...), (...)\)", line)[0]
10        paths[name] = (l, r)
11
12cur = "AAA"
13for i, c in enumerate(cycle(directions), start=1):
14    cur = paths[cur][c == "R"]
15    if cur == "ZZZ":
16        print(i)
17        break
18
19starts = [s for s in paths.keys() if s.endswith("A")]
20found = [[] for _ in range(len(starts))]
21
22for s in starts:
23    t = (-1, "AAA")
24    h = (0, paths["AAA"][directions[0] == "R"])
25    while t != h:
26        i, t = t
27        j, h = h
28        i = i + 1 % len(directions)
29        t = (i, paths[t][directions[i] == "R"])
30        t = (i + 1, paths[t][directions[i + 1] == "R"])
31    print(t, 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
40print(found)
41print(starts)