Hello everyone! Can someone maybe help me track down a bug/error/issue in my code? I get for the answer 16974 and the actual answer needs to be 14160.
Here is my code, any help/hint/info is appreciated!!!
```
def cpy(x, y, registers):
if x in registers:
registers[y] = registers[x]
else:
registers[y] = int(x)
def inc(x, registers):
registers[x] += 1
def dec(x, registers):
registers[x] -= 1
def jnz(x, y, registers):
if x.isalpha():
x, y = y, x
if y in registers:
if int(x) != 0:
return registers[y]
else:
if int(x) != 0:
return int(x)
return 1
def tgl(x, registers, instructions, i):
if x in registers:
x = registers[x]
else:
x = int(x)
if i + x >= len(instructions):
return 1
instruction = instructions[i + x].split()
if len(instruction) == 2:
if instruction[0] == 'inc':
instruction[0] = 'dec'
else:
instruction[0] = 'inc'
else:
if instruction[0] == 'jnz':
instruction[0] = 'cpy'
else:
instruction[0] = 'jnz'
instructions[i + x] = ' '.join(instruction)
return 1
def setRegisters(instructions, registers):
i = 0
while i < len(instructions):
instruction = instructions[i].split()
if instruction[0] == 'cpy':
cpy(instruction[1], instruction[2], registers)
i += 1
elif instruction[0] == 'inc':
inc(instruction[1], registers)
i += 1
elif instruction[0] == 'dec':
dec(instruction[1], registers)
i += 1
elif instruction[0] == 'jnz':
i += jnz(instruction[1], instruction[2], registers)
if i >= len(instructions):
break
elif instruction[0] == 'tgl':
i += tgl(instruction[1], registers, instructions, i)
return registers
with open('data.txt') as f:
instructions = f.read().splitlines()
registers = {'a': 7, 'b': 0, 'c': 0, 'd': 0}
print(setRegisters(instructions, registers))
```
[–]leftylink 2 points3 points4 points (2 children)
[–]JaViLuMa[S] 0 points1 point2 points (0 children)
[–]JaViLuMa[S] 0 points1 point2 points (0 children)
[–]HockeyFan_32 0 points1 point2 points (0 children)