-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent08.py
executable file
·97 lines (79 loc) · 2.38 KB
/
advent08.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
93
94
95
96
97
#!/usr/bin/env python3
"""
https://adventofcode.com/2023/day/8
Usage:
cat advent08.input | ./advent08.py
"""
import sys
import re
import numpy as np
def main ():
input = sys.stdin.read().strip().split('\n')
(instructions, network) = parse_input(input)
print("Part 1:", part1(instructions, network))
# 18827
print("Part 2:", part2(instructions, network))
# 20220305520997
def part1 (instructions, network):
steps = 0
instructions = Instructions(instructions)
field = "AAA"
for instruction in instructions:
if field == "ZZZ":
break
left, right = network[field]
if instruction == "L":
field = left
elif instruction == "R":
field = right
steps += 1
#print(steps, field, instruction, instructions.pointer)
return steps
def part2 (instructions, network):
instructions = Instructions(instructions)
fields = [x for x in network.keys() if x.endswith("A")]
steps_for_field = []
# print(fields)
for field in fields:
#print("Starting with", field)
instructions.pointer = 0
steps = 0
for instruction in instructions:
left, right = network[field]
if instruction == "L":
field = left
elif instruction == "R":
field = right
steps += 1
if field.endswith("Z"):
#print("Found", field, "in", steps, "steps")
break
steps_for_field.append(steps)
#print(steps_for_field)
arr = np.array(steps_for_field)
return np.lcm.reduce(arr)
def parse_input (input):
instructions = input[0]
network = {}
for line in input[2:]:
matches = re.match(r'^(\w+) = \((\w+), (\w+)\)$', line)
if matches:
(field, left, right) = matches.groups()
network[field] = (left, right)
else:
print("Error parsing line:", line)
return instructions, network
class Instructions:
def __init__ (self, instructions):
self.instructions = instructions
def __iter__ (self):
self.pointer = 0
return self
def __next__ (self):
if self.pointer >= len(self.instructions):
self.pointer = 0
instruction = self.instructions[self.pointer]
self.pointer += 1
return instruction
if __name__ == '__main__':
main()