Roderic Day 4 lat temu
rodzic
commit
ba5c07fa53
1 zmienionych plików z 36 dodań i 0 usunięć
  1. +36
    -0
      y2020/p08.py

+ 36
- 0
y2020/p08.py Wyświetl plik

@@ -0,0 +1,36 @@
import re
import sys


def run(text):
acc = 0
pos = 0
seen = set()
instructions = text.splitlines()
while pos not in seen:
seen.add(pos)
inst, n = instructions[pos].split()
if inst == 'acc':
acc += int(n)
pos += 1
elif inst == 'jmp':
pos += int(n)
elif inst == 'nop':
pos += 1

if pos == len(instructions):
raise RuntimeError(acc)

return acc


text = sys.stdin.read()
print(run(text))
for match in re.finditer(r'(jmp|nop)', text):
seen, (a, b) = match.group(0), match.span()
pair, = {'jmp', 'nop'} - {seen}
var = text[:a] + pair + text[b:]
try:
run(var)
except RuntimeError as error:
print(error)

Ładowanie…
Anuluj
Zapisz