-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.py
More file actions
106 lines (81 loc) · 3.12 KB
/
Copy path25.py
File metadata and controls
106 lines (81 loc) · 3.12 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
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
98
99
100
101
102
103
104
105
106
import re
class Core(object):
def __init__(self, state):
self.tape = [0]
self.cursor = 0
self.state = state
def move_left(self):
if self.cursor == 0:
self.tape.insert(0,0)
else:
self.cursor -= 1
def move_right(self):
if self.cursor == len(self.tape) - 1:
self.tape.append(0)
self.cursor += 1
def read(self):
return self.tape[self.cursor]
def write(self, value):
self.tape[self.cursor] = value
def checksum(self):
return sum(self.tape)
def set_state(self, state):
self.state = state
class Write(object):
def __init__(self, value):
self.value = value
def run(self, core):
core.write(self.value)
class MoveRight(object):
def run(self, core):
core.move_right()
class MoveLeft(object):
def run(self, core):
core.move_left()
class SetState(object):
def __init__(self, state):
self.state = state
def run(self, core):
core.set_state(self.state)
if __name__ == '__main__':
with open('input_25.txt') as f:
content = f.readlines()
lines = [l.strip() for l in content]
state = re.match(r"Begin in state (\w)\.", lines[0]).groups()[0]
num_steps = int(re.match(r"Perform a diagnostic checksum after (\d+) steps\.", lines[1]).groups()[0])
line_idx = 3
blueprint = {}
while line_idx < len(lines):
state_key = re.match(r"In state (\w):", lines[line_idx]).groups()[0]
blueprint[state_key] = { 0:[], 1:[] }
line_idx += 1
while line_idx < len(lines) and lines[line_idx]:
val_key = int(re.match(r"If the current value is (\d):", lines[line_idx]).groups()[0])
line_idx += 1
while line_idx < len(lines) and lines[line_idx].startswith('-'):
write_match = re.match(r"- Write the value (\d)\.", lines[line_idx])
move_match = re.match(r"- Move one slot to the (left|right)\.", lines[line_idx])
continue_match = re.match(r"- Continue with state (\w)\.", lines[line_idx])
if write_match:
value = int(write_match.groups()[0])
blueprint[state_key][val_key].append(Write(value))
elif move_match:
direction = move_match.groups()[0]
if direction == 'right':
blueprint[state_key][val_key].append(MoveRight())
else:
blueprint[state_key][val_key].append(MoveLeft())
elif continue_match:
value = continue_match.groups()[0]
blueprint[state_key][val_key].append(SetState(value))
else:
raise Exception('Should not be here.')
line_idx += 1
line_idx += 1
core = Core(state)
for i in range(num_steps):
val = core.read()
state = core.state
for fn in blueprint[state][val]:
fn.run(core)
print('Part 1:', core.checksum())