You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
980 B
29 lines
980 B
lines = open('inputs/day9.txt').readlines()
|
|
|
|
def differences(line: str) -> list[str]:
|
|
new_line = [int(x) for x in line.strip('\n').split(' ')]
|
|
diffs = [new_line]
|
|
while sum([abs(x) for x in new_line]) != 0:
|
|
new_line = [new_line[i+1] - new_line[i] for i in range(len(new_line)-1)]
|
|
diffs.append(new_line)
|
|
return diffs
|
|
|
|
def predict_next(diffs: list[int]) -> int:
|
|
for i in range(1, len(diffs))[::-1]:
|
|
diffs[i-1].append(diffs[i-1][-1] + diffs[i][-1])
|
|
return diffs[0][-1]
|
|
|
|
def predict_first(diffs: list[int]) -> int:
|
|
for i in range(1, len(diffs))[::-1]:
|
|
diffs[i-1].insert(0, diffs[i-1][0] - diffs[i][0])
|
|
return diffs[0][0]
|
|
|
|
predictions_next = []
|
|
predictions_first = []
|
|
for line in lines:
|
|
diffs = differences(line)
|
|
predictions_next.append(predict_next(diffs))
|
|
predictions_first.append(predict_first(diffs))
|
|
|
|
print(f"Solution Part 1: {sum(predictions_next)}")
|
|
print(f"Solution Part 1: {sum(predictions_first)}") |