#!/usr/bin/python

import computer
import copy

def get_possible_change_locations(instructions):
    change_locations=[]
    instruction_location=0
    for instruction,param in instructions:
        if instruction == "nop":
            change_locations.append(instruction_location)
        elif instruction == "jmp":
            change_locations.append(instruction_location)
        instruction_location+=1
    return change_locations

def main():
    # read the instructions
    instructions=[line.rstrip().split(" ") for line in open("input.txt", "r")]
    possible_change_locations=get_possible_change_locations(instructions)

    for change_location in possible_change_locations:
        new_instructions=copy.deepcopy(instructions)
        instruction = new_instructions[change_location][0]
        param = new_instructions[change_location][1]
        if instruction == "nop":
            instruction = "jmp"
        else:
            instruction = "nop"

        new_instructions[change_location][0]=instruction

        comp=computer.Computer(new_instructions,break_on_exec=10)
        acc=comp.run()
        if comp.exited_on_break():
            continue
        else:
            print ("Replaced instruction at pos: {}, computer terminated normally with acc: {}".format(change_location, acc))
            break

if __name__ == "__main__":
    main()
