Files
aoc-in-haskell-2024/02/silver.hs
2024-12-02 12:34:17 +01:00

21 lines
609 B
Haskell

import Debug.Trace
debug = flip trace
isSortedGradually :: (Integer -> Integer -> Bool) -> [Integer] -> Bool
isSortedGradually f (x1:x2:xs) = (f x1 x2) && diff >= 1 && diff <= 3 && isSortedGradually f (x2:xs)
where diff = abs (x1 - x2)
isSortedGradually f (x1:_) = True
isSortedGradually f _ = True
isAscending :: [Integer] -> Bool
isAscending = isSortedGradually (<)
isDescending :: [Integer] -> Bool
isDescending = isSortedGradually (>)
ok xs = isAscending xs || isDescending xs
main :: IO()
main = interact $ show . sum . map (fromEnum . ok . map (read :: (String -> Integer)) . words) . lines