-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.py
executable file
·69 lines (44 loc) · 1.49 KB
/
08.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/env python3
# Advent of Code 23 - Day 08
from pathlib import Path
from tools import readfile
def parse_input(contents: list[str]) -> tuple[list[int], dict]:
instructions = [
int(i) for i in list(contents[0].replace("L", "0").replace("R", "1"))
]
reference = dict()
for line in contents[2:]:
k, raw_v = line.split(" = ")
v = tuple(raw_v.strip("()").split(", "))
reference[k] = v
return instructions, reference
def p1(contents: list[str]) -> int:
instructions, reference = parse_input(contents)
key = "AAA"
i = 0 # instruction pointer
while key != "ZZZ":
idx = instructions[i % len(instructions)]
key = reference[key][idx]
i += 1
return i
def p2(contents: list[str]) -> int:
instructions, reference = parse_input(contents)
# check = lambda v: lambda k: k[2] == v # check(n) -> lambda k: k[2] == n
keys = list(filter(lambda k: k[2] == "A", reference.keys()))
i = 0 # instruction pointer
while not all(k[2] == "Z" for k in keys):
newkeys = list()
for key in keys:
idx = instructions[i % len(instructions)]
newkeys.append(reference[key][idx])
i += 1
keys = newkeys
return i
if __name__ == "__main__":
fp = Path("inputs/08.txt")
# fp = Path("samples/08-1.txt")
contents = readfile(fp)
print(p1(contents)) # x=17621
# fp = Path("samples/08-2.txt")
contents = readfile(fp)
print(p2(contents))