Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

37 lines
766B

  1. import re
  2. import sys
  3. def run(text):
  4. acc = 0
  5. pos = 0
  6. seen = set()
  7. instructions = text.splitlines()
  8. while pos not in seen:
  9. seen.add(pos)
  10. inst, n = instructions[pos].split()
  11. if inst == 'acc':
  12. acc += int(n)
  13. pos += 1
  14. elif inst == 'jmp':
  15. pos += int(n)
  16. elif inst == 'nop':
  17. pos += 1
  18. if pos == len(instructions):
  19. raise RuntimeError(acc)
  20. return acc
  21. text = sys.stdin.read()
  22. print(run(text))
  23. for match in re.finditer(r'(jmp|nop)', text):
  24. seen, (a, b) = match.group(0), match.span()
  25. pair, = {'jmp', 'nop'} - {seen}
  26. var = text[:a] + pair + text[b:]
  27. try:
  28. run(var)
  29. except RuntimeError as error:
  30. print(error)