-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jasper Smit
committed
Dec 9, 2023
1 parent
6e6650e
commit 43a32f4
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import typer | ||
import math | ||
from itertools import pairwise | ||
|
||
|
||
|
||
def predict(sequence: list[int]) -> int: | ||
if all([x == 0 for x in sequence]): | ||
return 0 | ||
else: | ||
next_sequence = [] | ||
for i in pairwise(sequence): | ||
next_sequence.append(i[1] - i[0]) | ||
return i[-1] + predict(next_sequence) | ||
|
||
def sum_predictions(sequence: list[int]) -> int: | ||
sum_predictions = 0 | ||
for s in sequence: | ||
sum_predictions += predict(s) | ||
return sum_predictions | ||
|
||
|
||
def main(input_file: typer.FileText): | ||
input = input_file.read().split("\n") | ||
|
||
sequences = [] | ||
for row in input: | ||
if row == "": | ||
continue | ||
sequence = [int(x) for x in row.split(" ") if x != ""] | ||
sequences.append(sequence) | ||
|
||
print(f"Part 1: {sum_predictions(sequences)}") | ||
|
||
sequences_reversed = sequences | ||
for s in sequences_reversed: | ||
s.reverse() | ||
print(f"Part 2: {sum_predictions(sequences_reversed)}") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |