state = [3, 7] puzzle_in = '077201' elf_1 = 0 elf_2 = 1 def print_state(): out = '' for i, el in enumerate(state): if i == elf_1: out += '(' + str(el) + ')' elif i == elf_2: out += '[' + str(el) + ']' else: out += ' ' + str(el) + ' ' print(out) while 1: val_1 = state[elf_1] val_2 = state[elf_2] state.extend([ int(x) for x in list(str(val_1 + val_2)) ]) test_1 = state[-6:] test_2 = state[-7:-1] # print(''.join([str(x) for x in test_1]), ''.join([str(x) for x in test_2]), puzzle_in) if ''.join([str(x) for x in test_1]) == puzzle_in: res = len(state) - 6 break if ''.join([str(x) for x in test_2]) == puzzle_in: res = len(state) - 7 break elf_1 = (1 + elf_1 + val_1) % len(state) elf_2 = (1 + elf_2 + val_2) % len(state) # print_state() print(res)