--- /dev/null
+#!/usr/bin/python3
+
+class Computer:
+
+ def __init__(self, instructions=[], break_on_exec=0):
+ self.__acc__=0
+ self.__pos__=0
+ self.__instructions__=instructions
+ self.__executed_count__={}
+ self.__keep_running__=True
+ self.__break_on_exec_number__=break_on_exec
+ self.__exited_on_break__=False
+
+ def set_instructions(self, instructions):
+ self.__instructions__=instructions
+
+ def run(self):
+ while self.keep_running():
+ self.run_command()
+ # when we're done, return the accumulator
+ return self.__acc__
+
+ def keep_running(self):
+ if self.__pos__ >= len(self.__instructions__):
+ self.__keep_running__=False
+ return self.__keep_running__
+
+ def exited_on_break(self):
+ return self.__exited_on_break__
+
+ def run_command(self):
+ if self.__pos__ in self.__executed_count__:
+ self.__executed_count__[self.__pos__]+=1
+ else:
+ self.__executed_count__[self.__pos__]=1
+
+ if self.__break_on_exec_number__ > 0 and self.__executed_count__[self.__pos__] >= self.__break_on_exec_number__:
+ self.__keep_running__=False
+ self.__exited_on_break__=True
+ return
+ else:
+ (instruction,param) = self.__instructions__[self.__pos__]
+ if instruction == "nop":
+ # do nothing, except increment the program counter
+ self.__pos__+=1
+ elif instruction == "acc":
+ val=int(param)
+ self.__acc__+=val
+ self.__pos__+=1
+ elif instruction == "jmp":
+ val=int(param)
+ self.__pos__+=val
+ else:
+ raise Exception("WTAF - that's not a real instruction!")
+ return
+
+def main():
+ instructions = [line.rstrip().split(" ") for line in open("input.txt", "r")]
+ comp = Computer(instructions, break_on_exec=2)
+
+ print("Acc was {} when computer finished.".format(comp.run()))
+
+if __name__ == "__main__":
+ main()