summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormhsn <mail@mhsn.net>2024-12-02 12:00:00 +0000
committermhsn <mail@mhsn.net>2024-12-02 12:00:00 +0000
commit18e156fa778b7ed73ce3b532ed09785171fdc75f (patch)
tree5e5b84ed9e6310bed129e8ea526637f5ca6ea879
parent326242bf41ba4da4f43f1509cae59a7817994c1c (diff)
downloadaoc-18e156fa778b7ed73ce3b532ed09785171fdc75f.tar.gz
aoc-18e156fa778b7ed73ce3b532ed09785171fdc75f.zip
2024-02 python p1,p2
-rw-r--r--2024/02/python/main.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/2024/02/python/main.py b/2024/02/python/main.py
new file mode 100644
index 0000000..b4c8c9a
--- /dev/null
+++ b/2024/02/python/main.py
@@ -0,0 +1,22 @@
+from fileinput import input
+
+reports = [[int(level) for level in report.split()] for report in input()]
+
+
+def incr(xs):
+ return all(x < y and y - x <= 3 for x, y in zip(xs, xs[1:]))
+
+
+def safe(xs):
+ return incr(xs) or incr(xs[::-1])
+
+
+def drops(xs):
+ return (xs[:idx] + xs[idx + 1 :] for idx, _ in enumerate(xs))
+
+
+silver = sum(safe(rep) for rep in reports)
+gold = sum(any(safe(mod) for mod in drops(rep)) for rep in reports)
+
+print("silver:", silver)
+print("gold:", gold)