tag8 = open('input', 'r') tag8 = [x.strip() for x in tag8] def run_game(): acc = 0 zeiger = 0 besucht = list() print('=' * 10, 'LOOP START', '=' * 10) while zeiger < len(tag8): if zeiger in besucht: print('=' * 10, 'LOOP END. loop detected', '=' * 10) return False zeile = tag8[zeiger] besucht.append(zeiger) instruction = zeile[0:3] number = int(zeile[4:]) if instruction == 'jmp': zeiger += number else: zeiger += 1 if instruction == 'acc': acc += number print(zeile, 'zeiger', zeiger) print('=' * 10, 'LOOP END. Terminated', '=' * 10) return acc acc = False for zeilennummer, zeile in enumerate(tag8): instruction = zeile[0:3] rest = zeile[3:] if instruction == 'nop': tag8[zeilennummer] = 'jmp' + rest acc = run_game() if acc: print(f'Success: Changed line {zeile} to jmp') break tag8[zeilennummer] = 'nop' + rest elif instruction == 'jmp': tag8[zeilennummer] = 'nop' + rest acc = run_game() if acc: print(f'Success: Changed line {zeile} to nop') break tag8[zeilennummer] = 'jmp' + rest print(f'Processed line {zeile}') print('Acc nach der Schleife:', acc)