/ Python / 2016 / 12.py
12.py
 1  from lib import *
 2  
 3  input = read_input(2016, 12)
 4  
 5  program = input.splitlines()
 6  
 7  registers = {k: 0 for k in "abcd"}
 8  
 9  
10  def simulate():
11      pc = 0
12      while pc < len(program):
13          cmd, *args = program[pc].split()
14          x = registers[args[0]] if args[0] in registers else int(args[0])
15          if cmd == "cpy":
16              registers[args[1]] = x
17              pc += 1
18          elif cmd == "inc":
19              registers[args[0]] += 1
20              pc += 1
21          elif cmd == "dec":
22              registers[args[0]] -= 1
23              pc += 1
24          elif cmd == "jnz":
25              if x:
26                  pc += int(args[1])
27              else:
28                  pc += 1
29      return registers["a"]
30  
31  
32  print(simulate())
33  
34  
35  registers = {k: 0 for k in "abcd"}
36  registers["c"] = 1
37  print(simulate())