-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.1.py
69 lines (50 loc) · 1.31 KB
/
8.1.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
data = """
RL
AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)
"""
data = """
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)"""
data = open("8.txt").read()
class Node:
def __init__(self, val, left=None, right=None) -> None:
self.val = val
self.left = left
self.right = right
class Map:
def __init__(self, start: Node) -> None:
self.start = start
def iterate(self, insutrctions):
head = self.start
count = 0
while True:
for i in insutrctions:
count += 1
if i == "L":
head = head.left
else:
head = head.right
if head.val == "ZZZ":
return count
ins = list(data.strip().split("\n")[0].strip())
nodes: dict[str, Node] = dict()
for line in data.strip().split("\n\n")[1].split("\n"):
line = line.strip().split("=")
start = line[0].strip()
left, right = [x.strip() for x in line[1].strip().strip("(").strip(")").split(",")]
for x in [start, left, right]:
if x not in nodes:
nodes[x] = Node(x)
nodes[start].left = nodes[left]
nodes[start].right = nodes[right]
head = nodes["AAA"]
m = Map(head)
print(m.iterate(ins))