-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (55 loc) · 1.58 KB
/
main.py
File metadata and controls
66 lines (55 loc) · 1.58 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
65
66
import math
import re
if __name__ == "__main__":
test = """
"""
# network = {
# "AAA": ("BBB", "BBB"),
# "BBB": ("AAA", "ZZZ"),
# "ZZZ": ("ZZZ", "ZZZ")
# }
# Running day Day 8: Haunted Wasteland:
# 19783
# 9177460370549
# Start at node AAA
current_node = "AAA"
# Get the left/right instructions from the user
lines = [
i for i in open("../inputs/day08.txt", "r").read().split("\n") if i.strip()
]
instructions = lines[0]
network = {}
for row in lines[1:]:
# sample of line: `NFK = (LMH, RSS)`
m = re.search("([A-Z0-9]+) = \\(([A-Z0-9]+), ([A-Z0-9]+)\\)", row)
name, l, r = m.group(1), m.group(2), m.group(3)
network[name] = (l, r)
# print(network['NFK']) # ('LMH', 'RSS')
res = []
# starting point
pos = ["AAA"]
for pos in pos:
steps = 0
while pos != "ZZZ":
if instructions[steps % len(instructions)] == "L":
pos = network[pos][0]
else:
pos = network[pos][1]
steps += 1
res.append(steps)
print(f"part 1: {res[0]} ")
res = []
pos = [x for x in network if x.endswith("A")]
for pos in pos:
steps = 0
while True:
if instructions[steps % len(instructions)] == "L":
pos = network[pos][0]
else:
pos = network[pos][1]
if pos.endswith("Z"):
break
steps += 1
res.append(steps + 1)
result = math.lcm(*res)
print(f"part 2: {result}")