2023 - Day 9 part 2
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
kleph 2023-12-20 02:28:38 +01:00
parent d0693bbd7e
commit 3b81792506

42
2023/9/9_2.py Normal file
View file

@ -0,0 +1,42 @@
#!/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)