Advent of Code 2016 solutions https://adventofcode.com/2016/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12.py 790B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/python3
  2. with open('data/12.txt', 'r') as file:
  3. instr = list(map(str.split, map(str.strip, file.readlines())))
  4. def value(x):
  5. try:
  6. return int(x)
  7. except ValueError:
  8. return registers[x]
  9. def cpy(args): registers[args[1]] = value(args[0])
  10. def inc(args): registers[args[0]] += 1
  11. def dec(args): registers[args[0]] -= 1
  12. def jnz(args): registers['pc'] += 0 if value(args[0]) == 0 else value(args[1]) - 1
  13. def run():
  14. while registers['pc'] < len(instr):
  15. globals()[instr[registers['pc']][0]](instr[registers['pc']][1:])
  16. registers['pc'] += 1
  17. registers = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'pc': 0}
  18. run()
  19. print("Stage 1: %s" % registers['a'])
  20. registers = {'a': 0, 'b': 0, 'c': 1, 'd': 0, 'pc': 0}
  21. run()
  22. print("Stage 2: %s" % registers['a'])