main.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. file = open('input.txt','r')
  2. instructions = file.readlines()
  3. temp = []
  4. executed = []
  5. accu = 0
  6. def execute(line):
  7. global accu
  8. global executed
  9. if line == len(temp):
  10. print('finished '+temp[line-1].replace('\n',''))
  11. return True
  12. if line > len(temp) - 1 or line < 0:
  13. return False
  14. command = temp[line].replace('\n','')
  15. cmd = command.split(' ')
  16. if line in executed:
  17. return False
  18. executed.append(line)
  19. if cmd[0] == 'acc':
  20. accu += int(cmd[1])
  21. return execute(line + 1)
  22. if cmd[0] == 'nop':
  23. return execute(line + 1)
  24. if cmd[0] == 'jmp':
  25. return execute(line + int(cmd[1]))
  26. for j,i in enumerate(instructions):
  27. if i.split(' ')[0] == 'acc':
  28. continue
  29. if i.split(' ')[0] == 'nop':
  30. temp = instructions.copy()
  31. temp[j] = i.replace('nop','jmp')
  32. if i.split(' ')[0] == 'jmp':
  33. temp = instructions.copy()
  34. temp[j] = i.replace('jmp','nop')
  35. accu = 0
  36. executed = []
  37. if execute(0):
  38. print('swap '+str(j)+': '+ i +' and get accu of '+str(accu))