Solve day 9

This commit is contained in:
Nettika
2023-12-20 20:58:01 -08:00
parent a0e96c2500
commit 3375d25b92
3 changed files with 76 additions and 1 deletions

28
advent_of_code/oasis.py Normal file
View File

@@ -0,0 +1,28 @@
"Day 9: Mirage Maintenance"
Sequence = tuple[int, ...]
def parse_sequence(desc: str) -> Sequence:
return tuple(int(num) for num in desc.split(" "))
def extrapolate_sequence(sequence: tuple[int, ...]) -> int:
if any(sequence):
difference_sequence = tuple(
right - left for left, right in zip(sequence, sequence[1:])
)
next_difference = extrapolate_sequence(difference_sequence)
return sequence[-1] + next_difference
return 0
def solve_part_1(input: str) -> int:
sequences = (parse_sequence(desc) for desc in input.split("\n"))
return sum(extrapolate_sequence(sequence) for sequence in sequences)
def solve_part_2(input: str) -> int:
sequences = (parse_sequence(desc) for desc in input.split("\n"))
reversed_sequences = (tuple(reversed(sequence)) for sequence in sequences)
return sum(extrapolate_sequence(sequence) for sequence in reversed_sequences)