-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday18_duet.py
194 lines (153 loc) · 5.85 KB
/
day18_duet.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Advent of Code -- Day 18
http://adventofcode.com/2017/day/18"""
from collections import defaultdict, deque
from typing import NamedTuple
class NextInstruction(NamedTuple):
continue_program: bool
steps: int
waiting_to_receive: bool = False
class SoundCard(object):
def __init__(self):
self.values = defaultdict(int)
self.last_sound_played = None
self.recovers = None
def process_instruction(self, instruction_string):
instruction = instruction_string.split(' ')
command = instruction[0]
register = instruction[1]
if len(instruction) == 2:
if command == 'snd':
self.last_sound_played = self.values[register]
elif command == 'rcv':
if self.values[register] != 0:
self.recovers = self.last_sound_played
return NextInstruction(False, self.recovers, False)
else:
operation_with_register = instruction[2] in self.values
if operation_with_register:
value = self.values[instruction[2]]
else:
value = int(instruction[2])
if command == 'set':
self.values[register] = value
elif command == 'add':
self.values[register] += value
elif command == 'mul':
self.values[register] *= value
elif command == 'mod':
self.values[register] = self.values[register] % value
elif command == 'jgz':
if self.values[register] > 0:
return NextInstruction(True, value, False)
return NextInstruction(True, 1, False)
def calculate_value_of_recovered_frequency(instructions):
# set up computer
sound = SoundCard()
curr_line = 0
# loop thru instructions
while True:
continue_program, steps, waiting_to_receive = (
sound.process_instruction(instructions[curr_line]))
if continue_program is False:
return steps
curr_line += steps
stop_loop = curr_line < 0 or curr_line >= len(instructions)
if stop_loop:
break
class Computer(object):
def __init__(self, program_id, program_queue, other_queue):
self.program_id = program_id
# initialize registers
self.values = {}
for code in range(ord("a"), ord("z") + 1):
self.values[chr(code)] = 0
self.values['p'] = self.program_id
self.rcv_queue = program_queue
self.snd_queue = other_queue
self.send_count = 0
def process_instruction(self, instruction_string):
instruction = instruction_string.split(' ')
command = instruction[0]
register = instruction[1]
if len(instruction) == 2:
if command == 'snd':
try:
self.snd_queue.append(self.values[register])
except KeyError:
self.snd_queue.append(int(register))
self.send_count += 1
elif command == 'rcv':
if len(self.rcv_queue) == 0:
return NextInstruction(True, 0, True)
self.values[register] = self.rcv_queue.popleft()
else:
operation_with_register = instruction[2] in self.values
if operation_with_register:
value = self.values[instruction[2]]
else:
value = int(instruction[2])
if command == 'set':
self.values[register] = value
elif command == 'add':
self.values[register] += value
elif command == 'mul':
self.values[register] *= value
elif command == 'mod':
self.values[register] = self.values[register] % value
elif command == 'jgz':
try:
if self.values[register] > 0:
return NextInstruction(True, value, False)
except KeyError:
if int(register) > 0:
return NextInstruction(True, value, False)
return NextInstruction(True, 1, False)
def calculate_number_of_times_program1_sends_value(instructions):
program0_queue = deque()
program1_queue = deque()
program0 = Computer(0, program0_queue, program1_queue)
program1 = Computer(1, program1_queue, program0_queue)
curr_line_program0 = 0
curr_line_program1 = 0
while True:
continue_program0, steps_program0, waiting_to_receive0 = (
program0.process_instruction(instructions[curr_line_program0]))
continue_program1, steps_program1, waiting_to_receive1 = (
program1.process_instruction(instructions[curr_line_program1]))
# deadlock
if waiting_to_receive0 and waiting_to_receive1:
return program1.send_count
curr_line_program0 += steps_program0
curr_line_program1 += steps_program1
TEST_INPUT = """set a 1
add a 2
mul a a
mod a 5
snd a
set a 0
rcv a
jgz a -1
set a 1
jgz a -2"""
TEST_INPUT_DUET = """snd 1
snd 2
snd p
rcv a
rcv b
rcv c
rcv d"""
if __name__ == '__main__':
test_instructions = {}
for count, line in enumerate(TEST_INPUT.split('\n')):
test_instructions[count] = line
assert calculate_value_of_recovered_frequency(test_instructions) == 4
instructions = {}
with open('2017/data/day18_input.txt', 'r') as f:
for count, line in enumerate(f.readlines()):
instructions[count] = line.strip()
print(calculate_value_of_recovered_frequency(instructions))
test_instructions_problem2 = {}
for count, line in enumerate(TEST_INPUT_DUET.split('\n')):
test_instructions_problem2[count] = line
assert calculate_number_of_times_program1_sends_value(test_instructions_problem2) == 3
print(calculate_number_of_times_program1_sends_value(instructions))