-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday18.py
70 lines (55 loc) · 1.67 KB
/
day18.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
from collections import defaultdict, deque
with open('./inputs/18.txt') as f:
instructions = f.readlines()
interpret = lambda val, reg: reg[val] if val.isalpha() else int(val)
sent_counter = 0
def solve(registers, which, i=0, first_part=False):
played = 0
while i < len(instructions):
c = instructions[i].split()
v = interpret(c[-1], registers)
if c[0] == 'set':
registers[c[1]] = v
elif c[0] == 'add':
registers[c[1]] += v
elif c[0] == 'mul':
registers[c[1]] *= v
elif c[0] == 'mod':
registers[c[1]] %= v
elif c[0] == 'jgz':
w = interpret(c[1], registers)
if w > 0:
i += v
continue
elif c[0] == 'snd':
if first_part:
played = v
elif which == 'x':
y.append(v)
else:
global sent_counter
sent_counter += 1
x.append(v)
elif c[0] == 'rcv':
if first_part:
if v != 0:
return played
elif which == 'x' and x:
registers[c[-1]] = x.popleft()
elif which == 'y' and y:
registers[c[-1]] = y.popleft()
else:
return i
i += 1
print(solve(defaultdict(int), 'x', first_part=True))
registers_x = defaultdict(int)
registers_y = defaultdict(int)
registers_y['p'] = 1
x = deque([])
y = deque([])
x_i = solve(registers_x, 'x')
y_i = solve(registers_y, 'y')
while x or y:
x_i = solve(registers_x, 'x', x_i)
y_i = solve(registers_y, 'y', y_i)
print(sent_counter)