09_boost.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. import sys
  3. from operator import add, mul, lt, eq
  4. def run(p, get_input, memsize=0):
  5. def decode_param(offset):
  6. return p[pc + offset], modes // (10 ** (offset - 1)) % 10
  7. def pload(offset):
  8. param, mode = decode_param(offset)
  9. return param if mode == 1 else p[param + relbase * mode // 2]
  10. def pstore(offset, value):
  11. param, mode = decode_param(offset)
  12. p[param + relbase * mode // 2] = value
  13. opmap = {1: add, 2: mul, 7: lt, 8: eq}
  14. p = p + [0] * memsize
  15. pc = relbase = 0
  16. while p[pc] != 99:
  17. modes, opcode = divmod(p[pc], 100)
  18. if opcode in (1, 2, 7, 8):
  19. pstore(3, opmap[opcode](pload(1), pload(2)))
  20. pc += 4
  21. elif opcode == 3:
  22. pstore(1, get_input())
  23. pc += 2
  24. elif opcode == 4:
  25. yield pload(1)
  26. pc += 2
  27. elif opcode == 5:
  28. pc = pload(2) if pload(1) else pc + 3
  29. elif opcode == 6:
  30. pc = pload(2) if not pload(1) else pc + 3
  31. elif opcode == 9:
  32. relbase += pload(1)
  33. pc += 2
  34. program = list(map(int, sys.stdin.read().split(',')))
  35. print(next(run(program, [1].pop, 10000)))
  36. print(next(run(program, [2].pop, 10000)))