This commit is contained in:
parent
d0693bbd7e
commit
3b81792506
1 changed files with 42 additions and 0 deletions
42
2023/9/9_2.py
Normal file
42
2023/9/9_2.py
Normal 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)
|
Loading…
Reference in a new issue