35 lines
962 B
Python
35 lines
962 B
Python
from typing import Any, Iterator
|
|
|
|
from ..base import BaseSolver
|
|
|
|
|
|
class Solver(BaseSolver):
|
|
def solve(self, input: str) -> Iterator[Any]:
|
|
lines = input.splitlines()
|
|
|
|
data = [[int(c) for c in line.split()] for line in lines]
|
|
|
|
right_values: list[int] = []
|
|
left_values: list[int] = []
|
|
for values in data:
|
|
diffs = [values]
|
|
while any(d != 0 for d in diffs[-1]):
|
|
diffs.append(
|
|
[rhs - lhs for lhs, rhs in zip(diffs[-1][:-1], diffs[-1][1:])]
|
|
)
|
|
|
|
rhs: list[int] = [0]
|
|
lhs: list[int] = [0]
|
|
for cx in range(len(diffs) - 1):
|
|
rhs.append(diffs[-cx - 2][-1] + rhs[cx])
|
|
lhs.append(diffs[-cx - 2][0] - lhs[cx])
|
|
|
|
right_values.append(rhs[-1])
|
|
left_values.append(lhs[-1])
|
|
|
|
# part 1
|
|
yield sum(right_values)
|
|
|
|
# part 2
|
|
yield sum(left_values)
|