-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2308.py
81 lines (77 loc) · 1.67 KB
/
2308.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
70
71
72
73
74
75
76
77
78
79
F = 0
# 56922302683x . 2084338078790969416800 : hi
import math
r1, r2 = 0, 0
A,P=[],''
ok=False
with open('08.' + str(F)) as file:
for line in file:
line=line.strip()
if line == '':
ok=True
elif not ok:
P=line
else:
A.append(line)
L,R={},{}
D={}#,counter={},{}#defaultdict(str)#{}
t=0
plen= len(P)
for a in A:
node, tmp = a.split('=')
l,r = tmp.split(', ')
node,l,r = node[:-1], l[2:], r[:-1]
print(node,'>',l,r)
"""if 'L' not in D:
D['L']={}
if 'R' not in D:
D['R']={}"""
L[node],R[node] = l,r
#print('P:',P)
#for a in A:print(a)
# part 1
node = 'AAA'
while not node == 'ZZZ':
DIR = P[t % plen]
if DIR == 'L':
node = L[node]
else:
node = R[node]
t += 1
r1 = t
# part 2
A = []
counter = {}
for node in L: # L and R have same set of Keys
if node[-1] == 'A':
A.append(node)
t = 0
Rec = []
while 1:
Nodes = []
found = False
for i in range(len(A)):
node = A[i]
DIR = P[t % plen]
if DIR == 'L':
node = L[node]
else:
node = R[node]
if node[-1] == 'Z':
counter[i] = t + 1
if len(counter) == len(A): # all the A paths have been completed
found = True
tmp = [v for k,v in counter.items()]
res = 1
for v in tmp:
gcd = math.gcd(res, v)
res = res * v // gcd
Rec, r2 = tmp, res
break
Nodes.append(node)
if found:
break
A = Nodes.copy()
t += 1
print('part 1:', r1)
print('part 2:', r2, counter)