This repository was archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathday18.py
More file actions
executable file
·66 lines (57 loc) · 1.27 KB
/
day18.py
File metadata and controls
executable file
·66 lines (57 loc) · 1.27 KB
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
#!/usr/bin/env python3
import sys
d = [x.strip().split() for x in sys.stdin.readlines()]
def v(mem, x):
try:
return int(x)
except ValueError:
return mem.get(x, 0)
def g(q_in, q_out, x=0):
reg = {'p': q_in.pop(0)}
i = 0
while 0 <= i < len(d):
ins = d[i]
c = ins[0]
a = v(reg, ins[1])
if c == 'snd':
q_out.append(a)
elif c == 'rcv':
if x:
if a:
yield q_out[-1]
return
elif not q_in:
yield None
reg[ins[1]] = q_in.pop(0)
else:
b = v(reg, ins[2])
if c == 'set':
reg[ins[1]] = b
elif c == 'add':
reg[ins[1]] = a + b
elif c == 'mul':
reg[ins[1]] = a * b
elif c == 'mod':
reg[ins[1]] = a % b
elif c == 'jgz' and a > 0:
i += b - 1
i += 1
print(next(g([0], [], 1)))
aq, bq = [0], [1]
a = g(aq, bq)
b = g(bq, aq)
z = 0
while aq:
if aq:
try:
next(a)
except:
aq = None
if bq:
try:
next(b)
except:
break
finally:
z += len(aq)
print(z)