summaryrefslogtreecommitdiff
path: root/2-1.hs
diff options
context:
space:
mode:
authorLaura Orvokki Kursula <lav@vampires.gay>2024-12-03 22:06:56 +0100
committerLaura Orvokki Kursula <lav@vampires.gay>2024-12-03 22:06:56 +0100
commitadb8d55ccbc9aafa8949f1677e4784334ac695e7 (patch)
tree11baae93b72a5f413c4a2db781c7a735a07c7d1e /2-1.hs
parent1f6080dfae0b1ff748790f8b6afda15cab3117aa (diff)
downloadaoc2024-adb8d55ccbc9aafa8949f1677e4784334ac695e7.tar.gz
aoc2024-adb8d55ccbc9aafa8949f1677e4784334ac695e7.zip
2-1
Diffstat (limited to '2-1.hs')
-rw-r--r--2-1.hs28
1 files changed, 28 insertions, 0 deletions
diff --git a/2-1.hs b/2-1.hs
new file mode 100644
index 0000000..934b08f
--- /dev/null
+++ b/2-1.hs
@@ -0,0 +1,28 @@
+monotonic :: [Int] -> Bool
+monotonic xs = all (<0) xs || all (>0) xs
+
+gradual :: [Int] -> Bool
+gradual = all ((<= 3) . abs)
+
+differences :: [Int] -> [Int]
+differences (x:y:xs) = x-y : differences (y:xs)
+differences _ = []
+
+safe :: [Int] -> Bool
+safe xs = let ys = differences xs
+ in monotonic ys && gradual ys
+
+line :: String -> Bool
+line = safe . map read . words
+
+count :: (a -> Bool) -> [a] -> Int
+count p = foldr f 0
+ where
+ f x y = if p x then succ y else y
+
+main :: IO ()
+main = do
+ s <- getContents
+ let ls = lines s
+ let xs = map line ls
+ print $ count id xs