-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023 D8.py
92 lines (78 loc) · 2.3 KB
/
2023 D8.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
80
81
82
83
84
85
86
87
88
89
90
91
92
import math
day = 8
testing = False
f = open(f'inputs/{"samples" if testing else ""}/day{day}.txt').read().splitlines()
def part1():
instructions = f[0]
mod = len(instructions)
nodes = {}
for line in f[2:]:
nodes[line[0:3]] = [line[7:10], line[12:15]]
steps = 0
current = 'AAA'
i = 0
while current != 'ZZZ':
if instructions[i] == 'L':
current = nodes[current][0]
else:
current = nodes[current][1]
steps += 1
i = (i+1) % mod
return steps
def part2():
instructions = f[0]
mod = len(instructions)
nodes = {}
for line in f[2:]:
nodes[line[0:3]] = [line[7:10], line[12:15]]
steps = 0
currents = []
for n in nodes.keys():
if n[-1] == 'A': currents.append(n)
i = 0
while True:
if instructions[i] == 'L': dir = 0
else: dir = 1
steps += 1
print(steps)
j = 0
for cur in range(len(currents)):
currents[cur] = nodes[currents[cur]][dir]
if currents[cur][-1] == 'Z': j += 1
if len(currents) == j: return steps
i = (i+1) % mod
def compute_lcm(l):
least = 1
for i in range(len(l)):
least = math.lcm(least, l[i])
return least
def part2_attempt2():
instructions = f[0]
mod = len(instructions)
nodes = {}
for line in f[2:]:
nodes[line[0:3]] = [line[7:10], line[12:15]]
steps = 0
currents = {}
ZSteps, cycleSteps = -1, -1
for n in nodes.keys():
if n[-1] == 'A': currents[n] = [n, ZSteps, cycleSteps]
i = 0
while True:
if instructions[i] == 'L': dir = 0
else: dir = 1
steps += 1
done = True
for node in currents.keys():
next = nodes[currents[node][0]][dir]
currents[node][0] = next
if currents[node][0][-1] == 'Z' and currents[node][1] == -1:
currents[node][1] = steps
if next in currents[node][0] and currents[node][2] == -1:
currents[node][2] = steps
if -1 in currents[node]: done = False
if done: break
i = (i+1) % mod
return compute_lcm([currents[n][1] for n in currents.keys()])
print('Part 1 Answer:', part1())
print('Part 2 Answer:', part2_attempt2())