You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB
Python

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)