with open(r'day3/input.txt', 'r') as input: lines = input.read().split('\n')[:-1] sum = 0 for line in lines: # Transforming line to array of digits numbers = list(map(lambda x: int(x), list(line))) besti = -1 # -1 to give 0 to the start of the allowed ranges linejolt = '' # value buffer for i in range(12): # Allowed is the list of elements we can choose from while not compromising future choices # 100 magic number to prevent 11-i from becoming negative, hence excluding last number if list has one single element remaining allowed = numbers[besti+1:-(11-i) if 11-i > 0 else 100] best = max(allowed) # slicing here to prevent chosing index from a previous allowed range besti = numbers[besti+1:].index(best)+besti+1 linejolt += str(best) sum += int(linejolt) print(sum)