Last active
December 8, 2020 19:02
-
-
Save rtt/a0737bebe24a1c5bc6853c16988772ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
def get_input(): | |
with open('./input8.txt') as inp: | |
return inp.read().strip().split('\n') | |
def finishes(ops): | |
visited = set() | |
acc = 0 | |
cursor = 0 | |
while True: | |
if cursor in visited: | |
return False, None | |
if cursor > len(ops) -1: | |
return True, acc | |
visited.add(cursor) | |
op, oper = ops[cursor].split(' ') | |
if op == 'acc': | |
acc += int(oper) | |
cursor += 1 | |
elif op == 'jmp': | |
cursor += int(oper) | |
else: | |
cursor += 1 | |
def alter(ops, i): | |
r = [] | |
for ix, op in enumerate(ops): | |
cmd, oper = op.split(' ') | |
if cmd in ['jmp', 'nop'] and ix == i: | |
r.append(' '.join(['nop' if cmd == 'jmp' else 'jmp', oper])) | |
else: | |
r.append(op) | |
return r | |
def run(): | |
ops, i = get_input(), 0 | |
while True: | |
finished, acc = finishes(alter(ops, i)) | |
if finished: | |
return acc | |
i += 1 | |
if __name__ == "__main__": | |
print(run()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment