瀏覽代碼

2020/8

master
Roderic Day 4 年之前
父節點
當前提交
ba5c07fa53
共有 1 個檔案被更改,包括 36 行新增0 行删除
  1. +36
    -0
      y2020/p08.py

+ 36
- 0
y2020/p08.py 查看文件

@@ -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)

Loading…
取消
儲存