summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2024-12-02 09:44:14 +0100
committerTom Smeding <tom@tomsmeding.com>2024-12-02 09:44:14 +0100
commitff973cbb34f03be0c570a4d16aaf8c173d1e7c7e (patch)
tree7ac6f6c7773937ae6b5488a395a8042b0fdd24b6
parent86752b923527c0482b067faae1476127589b9f44 (diff)
2
-rw-r--r--2024/2.hs12
1 files changed, 12 insertions, 0 deletions
diff --git a/2024/2.hs b/2024/2.hs
new file mode 100644
index 0000000..e29acec
--- /dev/null
+++ b/2024/2.hs
@@ -0,0 +1,12 @@
+import Data.List (inits, tails)
+
+main :: IO ()
+main = do
+ input <- map (map (read @Int) . words) . lines <$> getContents
+ let safePair a b = 1 <= b - a && b - a <= 3
+ let safeRep l = all (uncurry safePair) (zip l (tail l)) ||
+ all (uncurry (flip safePair)) (zip l (tail l))
+ print $ sum [fromEnum (safeRep l) | l <- input]
+ let dampened l = zipWith (++) (inits l) (tail (tails l))
+ let safeRep' l = any safeRep (dampened l)
+ print $ sum [fromEnum (safeRep' l) | l <- input]