5 def __init__(self, instructions=[], break_on_exec=0):
8 self.__instructions__=instructions
9 self.__executed_count__={}
10 self.__keep_running__=True
11 self.__break_on_exec_number__=break_on_exec
12 self.__exited_on_break__=False
14 def set_instructions(self, instructions):
15 self.__instructions__=instructions
18 while self.keep_running():
20 # when we're done, return the accumulator
23 def keep_running(self):
24 if self.__pos__ >= len(self.__instructions__):
25 self.__keep_running__=False
26 return self.__keep_running__
28 def exited_on_break(self):
29 return self.__exited_on_break__
31 def run_command(self):
32 if self.__pos__ in self.__executed_count__:
33 self.__executed_count__[self.__pos__]+=1
35 self.__executed_count__[self.__pos__]=1
37 if self.__break_on_exec_number__ > 0 and self.__executed_count__[self.__pos__] >= self.__break_on_exec_number__:
38 self.__keep_running__=False
39 self.__exited_on_break__=True
42 (instruction,param) = self.__instructions__[self.__pos__]
43 if instruction == "nop":
44 # do nothing, except increment the program counter
46 elif instruction == "acc":
50 elif instruction == "jmp":
54 raise Exception("WTAF - that's not a real instruction!")
58 instructions = [line.rstrip().split(" ") for line in open("input.txt", "r")]
59 comp = Computer(instructions, break_on_exec=2)
61 print("Acc was {} when computer finished.".format(comp.run()))
63 if __name__ == "__main__":