advent_of_code/2023/9/9.py
2024-01-24 04:57:07 +01:00

42 lines
1 KiB
Python

#!/usr/bin/env python
# 2023 - Advent Of Code 9
# 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].append(0)
else:
d = diffs_list[seq+1][-1] + diffs_list[seq][-1]
# print(f'{diffs_list[seq + 1][-1]} + {diffs_list[seq][-1]} = {d}')
diffs_list[seq].append(d)
# print(f'diffs: {diffs_list[seq]}')
res += d
print()
print(res)