with open('input20.txt','r') as f: inp = f.read().splitlines(keepends=False) sequence = [] for i in range(len(inp)): if int(inp[i]) == 0: zero = (i, int(inp[i])) sequence.append((i, int(inp[i]))) numbers = sequence.copy() for number in sequence: if sequence.count(number) > 1: print('!') for number in numbers: #print(number[0]) index = sequence.index(number) _number = number[1] newIndex = index + _number while not (len(sequence) * -1) <= newIndex < len(sequence): if newIndex < (len(sequence) * -1): newIndex += len(sequence) if newIndex >= len(sequence): newIndex -= len(sequence) - 1 if newIndex == 0: newIndex = len(sequence) - 1 elif newIndex < 0: newIndex = len(sequence) -1 + newIndex if _number >= len(sequence): newIndex += 1 if _number <= (len(sequence)* -1): newIndex -= 1 if newIndex > index: newIndex += 1 sequence.insert(newIndex, number) sequence.pop(index) else: sequence.pop(index) sequence.insert(newIndex, number) #print(sequence) number = sequence.index(zero) + 1000 _sum = 0 for i in range(3): number %= len(sequence) #print(sequence[number]) _sum += sequence[number][1] number += 1000 print(_sum)