#!/usr/bin/env python # 2023 - Advent Of Code 9 - part 2 # file = 'input_example.txt' file = 'input.txt' def all_zeroes(lst): for i in lst: if i != 0: return False return True # pylint: disable=consider-using-with input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")] res = 0 for line in input_lines: sequence = [int(x) for x in line.split()] print(sequence) diffs_list = [sequence] while not all_zeroes(sequence): diffs = [sequence[s+1] - sequence[s] for s in range(0, len(sequence)-1)] diffs_list.append(diffs) sequence = diffs # print(diffs_list) for seq in range(len(diffs_list)-1, -1, -1): if seq == len(diffs_list)-1: diffs_list[seq].insert(0, 0) else: d = diffs_list[seq][0] - diffs_list[seq + 1][0] # print(f'{diffs_list[seq][0]} - {diffs_list[seq + 1][0]} = {d}') diffs_list[seq].insert(0, d) # print(f'diffs: {diffs_list[seq]}') res += d print() print(res)