instructions = [l.split(" ") for l in open("input", "r").read().split("\n") if l] instructions = [(operation, int(argument)) for (operation, argument) in instructions] #jumpsto = {i: set() for i in range(0, len(instructions)+1)} #jumpsfrom = {i: set() for i in range(0, len(instructions)+1)} def follow(i, changed, visited): if i == len(instructions): print("changed", changed) return if i in visited: return (opcode, p) = instructions[i] visited = visited + [i] if opcode == "nop": follow(i + 1, changed, visited) if not changed: follow(i + p, i, visited) if opcode == "jmp": if not changed: follow(i + 1, i, visited) follow(i + p, changed, visited) elif opcode == "acc": follow(i + 1, changed, visited) follow(0, 0, [])