21_springdroid.py 821 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python3
  2. import sys
  3. from intcode import read_program, run_inputs
  4. def run_springscript(program, script):
  5. inputs = map(ord, script.lstrip())
  6. interp = run_inputs(program, inputs)
  7. return next(out for out in interp if out > 128)
  8. # Jump if A or C is a hole and D is reachable:
  9. # J = (!A | !C) & D
  10. walkscript = '''
  11. NOT A T
  12. NOT C J
  13. OR T J
  14. AND D J
  15. WALK
  16. '''
  17. # Jump if A is a hole, or B/C are holes and D is reachable, but we don't end up
  18. # just before a hole at which we will have to jump but cannot:
  19. # J = !A | (D & (!B | !C) & !(!E & !H))
  20. # = (!(B & C) & (E | H) & D) | !A
  21. runscript = '''
  22. OR B T
  23. AND C T
  24. NOT T J
  25. NOT E T
  26. NOT T T
  27. OR H T
  28. AND T J
  29. AND D J
  30. NOT A T
  31. OR T J
  32. RUN
  33. '''
  34. program = read_program(sys.stdin)
  35. print(run_springscript(program, walkscript))
  36. print(run_springscript(program, runscript))