Day 2 silver star

This commit is contained in:
2024-12-02 12:34:17 +01:00
parent 2162be2dde
commit a85064efea
2 changed files with 26 additions and 0 deletions

20
02/silver.hs Normal file
View File

@@ -0,0 +1,20 @@
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

6
02/test.txt Normal file
View File

@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9