-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_a.py
executable file
·67 lines (51 loc) · 1.74 KB
/
part_a.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
#!/usr/bin/env python3
from collections import defaultdict
import utils
from year_2019.day_15.part_a import run_interactive_program
import year_2019.day_09.part_a
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
15969
"""
return run_network(_input, manage_output_first_to_255)
def run_network(_input, manage_output_func):
computers = [
run_interactive_program(_input, endless=True)
for _ in range(50)
]
queues = defaultdict(list)
for address, computer in enumerate(computers):
_, first_output = computer.send(None)
manage_output_func(address, first_output, queues)
_, second_output = computer.send(address)
manage_output_func(address, second_output, queues)
finished, result = False, None
while not finished:
for address, computer in enumerate(computers):
queue = queues[address]
if queue:
next_input = queue.pop(0)
else:
next_input = -1
_, computer_output = computer.send(next_input)
finished, result = manage_output_func(
address, computer_output, queues)
if finished:
break
if result is None:
raise Exception("Got empty result")
return result
def manage_output_first_to_255(from_address, output, queues):
if not output:
return False, None
# print(from_address, output)
while output:
(to_address, x, y), output = output[:3], output[3:]
if to_address == 255:
return True, y
queues[to_address].append((x, y))
return False, None
Challenge.main()
challenge = Challenge()