summaryrefslogtreecommitdiffstats
path: root/prog/aoc/23/8/1.py
blob: 9ff0113f0bfd46c4b94081af08537419626fc36b (plain) (blame)
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
#!/usr/bin/python3
inst = input()
input()
graph = {}
try:
	while True:
		a = input().split(" = ")
		graph[a[0]] = a[1].replace("(", "").replace(")", "").split(", ")
except EOFError:
	pass
cur = "AAA"
runs = 0
while True:
	for d in inst:
		if cur == "ZZZ":
			break
		if d == "L":
			cur = graph[cur][0]
		else:
			cur = graph[cur][1]
		runs += 1
	else:
		continue
	break
print(runs)
r = []
for s in [x for x in graph if x[2] == "A"]:
	runs = 0
	while True:
		for d in inst:
			if s[2] == "Z":
				break
			if d == "L":
				s = graph[s][0]
			else:
				s = graph[s][1]
			runs += 1
		else:
			continue
		break
	r.append(runs)
from math import lcm
print(lcm(*r))