summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-12-09 10:07:25 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-12-09 10:07:25 +0000
commitf0e81eadf75658eadbc3ce293ef6743b8fea8bc0 (patch)
tree83bda38deeaddcf1e06dd28d2e5542c39ea8b1bc
parent849ee38426c977e0dca8a5f43d5d97641d8a23a8 (diff)
downloadaoc2023-f0e81eadf75658eadbc3ce293ef6743b8fea8bc0.tar.gz
aoc2023-f0e81eadf75658eadbc3ce293ef6743b8fea8bc0.tar.xz
aoc2023-f0e81eadf75658eadbc3ce293ef6743b8fea8bc0.zip
day 9
-rw-r--r--9.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/9.py b/9.py
new file mode 100644
index 0000000..a98d67a
--- /dev/null
+++ b/9.py
@@ -0,0 +1,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))