02_intcode.py 727 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python3
  2. import sys
  3. from operator import add, mul
  4. def run(p):
  5. pc = 0
  6. opcode = p[pc]
  7. while p[pc] != 99:
  8. opcode, in1, in2, out = p[pc:pc + 4]
  9. op = add if opcode == 1 else mul
  10. p[out] = op(p[in1], p[in2])
  11. pc += 4
  12. def initrun(p, noun, verb):
  13. p = list(p)
  14. p[1:3] = noun, verb
  15. run(p)
  16. return p[0]
  17. def find_params(p, desired_result):
  18. noun = verb = 0
  19. while initrun(p, noun, verb) <= desired_result:
  20. noun += 1
  21. noun -= 1
  22. while initrun(p, noun, verb) < desired_result:
  23. verb += 1
  24. return 100 * noun + verb
  25. program = list(map(int, sys.stdin.read().split(',')))
  26. print(initrun(program, 12, 2))
  27. print(find_params(program, 19690720))