summaryrefslogtreecommitdiffstats
path: root/9.py
blob: a98d67a4b6324532e78d00691c9689f40f6810e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# pyright: strict
from sys import stdin


def predict_next(history: list[int]) -> int:
    if all(e == 0 for e in history):
        return 0
    return history[-1] + predict_next(
        [a - b for a, b in zip(history[1:], history[:-1])]
    )


def predict_prev(history: list[int]) -> int:
    if all(e == 0 for e in history):
        return 0
    return history[0] - predict_prev([a - b for a, b in zip(history[1:], history[:-1])])


histories = [[int(e) for e in l.rstrip().split()] for l in stdin]

print(sum(predict_next(history) for history in histories))
print(sum(predict_prev(history) for history in histories))