-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
executable file
·29 lines (24 loc) · 827 Bytes
/
day10.py
File metadata and controls
executable file
·29 lines (24 loc) · 827 Bytes
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
import re
from collections import defaultdict
with open('day10_input.txt') as f:
instructions = f.read()
H = re.findall(r'value (\d+) goes to (.+)', instructions)
has = defaultdict(set)
for tup in H:
has[tup[1]].add(int(tup[0]))
gives = {}
G = re.findall(r'(.+) gives low to (.+) and high to (.+)', instructions)
for tup in G:
gives[tup[0]] = (tup[1], tup[2])
def transfer(giver, getter, value):
has[giver].remove(value)
has[getter].add(value)
while not has['output 0']:
for bot in gives:
if len(has[bot]) > 1:
transfer(bot, gives[bot][0], min(has[bot]))
transfer(bot, gives[bot][1], max(has[bot]))
else:
print('output 2 has:', has['output 2'])
print('output 0 has:', has['output 0'])
print('output 1 has:', has['output 1'])