lines = list(map(lambda y: (y[0], int(y[1])), (x.strip().split() for x in open("input.txt")))) length = len(lines) for change_idx in range(length): old_value = lines[change_idx] if lines[change_idx][0] == 'nop': lines[change_idx] = ('jmp', lines[change_idx][1]) elif lines[change_idx][0] == 'jmp': lines[change_idx] = ('nop', lines[change_idx][1]) executed = set() pos = 0 acc = 0 while True: cmd, val = lines[pos] executed.add(pos) if cmd == 'nop': pos += 1 elif cmd == 'acc': acc += val pos += 1 elif cmd == 'jmp': pos += val if pos in executed: break if pos >= length: print('invalid index:', pos) print('changed index:', change_idx) print('current value:', acc) break lines[change_idx] = old_value