|
|
@@ -0,0 +1,48 @@
|
|
|
+file = open('input.txt','r')
|
|
|
+
|
|
|
+instructions = file.readlines()
|
|
|
+temp = []
|
|
|
+executed = []
|
|
|
+accu = 0
|
|
|
+
|
|
|
+
|
|
|
+def execute(line):
|
|
|
+ global accu
|
|
|
+ global executed
|
|
|
+ if line == len(temp):
|
|
|
+ print('finished '+temp[line-1].replace('\n',''))
|
|
|
+ return True
|
|
|
+ if line > len(temp) - 1 or line < 0:
|
|
|
+ return False
|
|
|
+ command = temp[line].replace('\n','')
|
|
|
+ cmd = command.split(' ')
|
|
|
+
|
|
|
+ if line in executed:
|
|
|
+ return False
|
|
|
+
|
|
|
+ executed.append(line)
|
|
|
+ if cmd[0] == 'acc':
|
|
|
+ accu += int(cmd[1])
|
|
|
+ return execute(line + 1)
|
|
|
+
|
|
|
+ if cmd[0] == 'nop':
|
|
|
+ return execute(line + 1)
|
|
|
+
|
|
|
+ if cmd[0] == 'jmp':
|
|
|
+ return execute(line + int(cmd[1]))
|
|
|
+
|
|
|
+for j,i in enumerate(instructions):
|
|
|
+ if i.split(' ')[0] == 'acc':
|
|
|
+ continue
|
|
|
+ if i.split(' ')[0] == 'nop':
|
|
|
+ temp = instructions.copy()
|
|
|
+ temp[j] = i.replace('nop','jmp')
|
|
|
+
|
|
|
+ if i.split(' ')[0] == 'jmp':
|
|
|
+ temp = instructions.copy()
|
|
|
+ temp[j] = i.replace('jmp','nop')
|
|
|
+
|
|
|
+ accu = 0
|
|
|
+ executed = []
|
|
|
+ if execute(0):
|
|
|
+ print('swap '+str(j)+': '+ i +' and get accu of '+str(accu))
|