소스 검색

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…
취소
저장