38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from functools import reduce
|
|
from typing import Iterable
|
|
|
|
from .aoc import Aoc2, AocParseLinesSameParser
|
|
|
|
|
|
class Day09(AocParseLinesSameParser[list[int]], Aoc2[list[list[int]], list[list[int]]]):
|
|
def parseline(self, inpt: str) -> list[int]:
|
|
return [int(d) for d in inpt.split(" ")]
|
|
|
|
def derive(self, init: list[int]) -> list[int]:
|
|
return [y - x for x, y in zip(init, init[1:])]
|
|
|
|
def get_nth_value_in_deriv(self, values: list[int], n: int) -> Iterable[int]:
|
|
while any(values):
|
|
yield values[n]
|
|
values = self.derive(values)
|
|
|
|
def part1(self, inpt: list[list[int]]) -> int:
|
|
return sum(sum(self.get_nth_value_in_deriv(line, -1)) for line in inpt)
|
|
|
|
def part2(self, inpt: list[list[int]]) -> int:
|
|
return sum(
|
|
reduce(
|
|
lambda acc, x: x - acc,
|
|
reversed(list(self.get_nth_value_in_deriv(line, 0))),
|
|
)
|
|
for line in inpt
|
|
)
|
|
|
|
|
|
def main() -> None:
|
|
day09 = Day09(2023, 9, example_code_nr2=0)
|
|
day09.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|