Repository navigation
Expand file tree
/
Copy path08-2.py
More file actions
64 lines (53 loc) · 1.48 KB
/
Copy path08-2.py
File metadata and controls
64 lines (53 loc) · 1.48 KB
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
import sys
import pytest
from pathlib import Path
import math
from textwrap import dedent
def solve(text: str) -> int:
lines = text.splitlines()
directions = lines[0]
nodes = {}
start_nodes = []
for line in lines[2:]:
node, _, neighbors = line.split(maxsplit=2)
neighbors = neighbors[1:-1].split(", ")
nodes[node] = neighbors
if node.endswith("A"):
start_nodes.append(node)
z_indexes = []
for start_node in start_nodes:
current_node = start_node
steps = 0
direction_index = 0
while not current_node.endswith("Z"):
direction = 0 if directions[direction_index] == "L" else 1
neighbors = nodes[current_node]
current_node = neighbors[direction]
direction_index = (direction_index + 1) % len(directions)
steps += 1
z_indexes.append(steps)
return math.lcm(*z_indexes)
if __name__ == "__main__":
p = Path(sys.argv[1])
print(solve(p.read_text()))
def test():
text = dedent(
"""
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)
"""
).strip()
assert solve(text) == 6
def test_input():
p = Path("08.txt")
if not p.exists():
pytest.skip(f"{p} does not exist")
text = p.read_text()
assert solve(text) == 21165830176709