[Python/2016] Move solutions into .py files
This commit is contained in:
parent
0269ad8fc3
commit
2514b1d11f
50 changed files with 1172 additions and 3386 deletions
52
Python/2016/25.py
Normal file
52
Python/2016/25.py
Normal file
|
@ -0,0 +1,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)
|
Loading…
Add table
Add a link
Reference in a new issue