-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_10.py
53 lines (38 loc) · 1.48 KB
/
day_10.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
from collections import defaultdict
with open('./inputs/10.txt', 'r') as infile:
instructions = infile.read().split('\n')
initial = [line.split() for line in instructions if line.startswith('value')]
commands = [line.split() for line in instructions if not line.startswith('value')]
connections = {}
for line in commands:
name, lower, higher = line[1], line[5:7], line[-2:]
connections[name] = (lower, higher)
bots = defaultdict(list)
outputs = defaultdict(list)
stack = []
def add_value(name, value):
bots[name].append(value)
if len(bots[name]) == 2:
stack.append(name)
def send_value(connection, value):
out_type, out_name = connection
if out_type == 'bot':
add_value(out_name, value)
else:
outputs[out_name].append(value)
for line in initial:
value, name = int(line[1]), line[-1]
add_value(name, value)
while stack:
name = stack.pop()
low_value, high_value = sorted(bots[name])
if low_value == 17 and high_value == 61:
wanted_bot = name
lower_connection, higher_connection = connections[name]
send_value(lower_connection, low_value)
send_value(higher_connection, high_value)
a, b, c = (outputs[i][0] for i in '012')
print(f"I'm Bot {wanted_bot}, and I'm responsible for microchips 17 and 61.")
print('....')
print(f"Outputs zero, one, and two have values: {a}, {b}, and {c}.")
print(f"Did you know that if you multiply them, you get {a*b*c}?")