-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.py
52 lines (41 loc) · 1.07 KB
/
25.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
from lib import *
input = read_input(2016, 25)
program = input.splitlines()
def simulate(a):
registers = {k: 0 for k in "abcd"}
registers["a"] = a
pc = 0
while pc < len(program):
cmd, x, y, *_ = program[pc].split() + [None]
read = lambda a: registers[a] if a in registers else int(a)
if cmd == "cpy":
if y in registers:
registers[y] = read(x)
pc += 1
elif cmd == "inc":
if x in registers:
registers[x] += 1
pc += 1
elif cmd == "dec":
if x in registers:
registers[x] -= 1
pc += 1
elif cmd == "jnz":
if read(x) and y is not None:
pc += read(y) or 1
else:
pc += 1
elif cmd == "out":
yield read(x)
pc += 1
def test(a):
for i, x in enumerate(simulate(a)):
if i % 2 != x:
return False
if i > 100:
return True
return False
a = 0
while not test(a):
a += 1
print(a)