[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
73
Python/2016/23.py
Normal file
73
Python/2016/23.py
Normal file
|
@ -0,0 +1,73 @@
|
|||
from lib import *
|
||||
|
||||
input = read_input(2016, 23)
|
||||
|
||||
program = input.splitlines()
|
||||
|
||||
registers = {k: 0 for k in "abcd"}
|
||||
|
||||
|
||||
def simulate():
|
||||
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 == "tgl":
|
||||
if pc + (x := read(x)) in range(len(program)):
|
||||
cmd, args = program[pc + x].split(maxsplit=1)
|
||||
|
||||
if cmd == "inc":
|
||||
cmd = "dec"
|
||||
|
||||
elif cmd in ["dec", "tgl"]:
|
||||
cmd = "inc"
|
||||
|
||||
elif cmd == "jnz":
|
||||
cmd = "cpy"
|
||||
|
||||
elif cmd in ["cpy"]:
|
||||
cmd = "jnz"
|
||||
|
||||
program[pc + x] = f"{cmd} {args}"
|
||||
|
||||
pc += 1
|
||||
|
||||
return registers["a"]
|
||||
|
||||
|
||||
registers["a"] = 7
|
||||
print(simulate())
|
||||
|
||||
|
||||
program = input.splitlines()
|
||||
registers = {k: 0 for k in "abcd"}
|
||||
registers["a"] = 7
|
||||
print(math.factorial(12) + simulate() - math.factorial(7))
|
Loading…
Add table
Add a link
Reference in a new issue