From 19bc483c5094484412881b9091ee4d4ad6800325 Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Mon, 2 Dec 2024 16:41:59 +0100 Subject: [PATCH] Update day 2 --- 02/gold.hs | 9 ++------- 02/silver.hs | 15 +++------------ 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/02/gold.hs b/02/gold.hs index 74bfd6c..95e33ef 100644 --- a/02/gold.hs +++ b/02/gold.hs @@ -1,3 +1,4 @@ + isSortedGradually' :: (Integer -> Integer -> Bool) -> Bool -> [Integer] -> Bool isSortedGradually' f mercy (x1:x2:xs) = (diff >= 1 && diff <= 3 && f x1 x2 && isSortedGradually' f mercy (x2:xs)) @@ -8,14 +9,8 @@ isSortedGradually' f mercy _ = True isSortedGradually f (x:xs) = isSortedGradually' f True (x:xs) || isSortedGradually' f False xs -isAscending :: [Integer] -> Bool -isAscending xs = isSortedGradually (<) xs - -isDescending :: [Integer] -> Bool -isDescending xs = isSortedGradually (>) xs - isSafe :: [Integer] -> Bool -isSafe xs = isAscending xs || isDescending xs +isSafe xs = isSortedGradually (<) xs || isSortedGradually (>) xs main :: IO() main = interact $ show . sum . map (fromEnum . isSafe . map read . words) . lines diff --git a/02/silver.hs b/02/silver.hs index d12aa19..a18ad0d 100644 --- a/02/silver.hs +++ b/02/silver.hs @@ -1,20 +1,11 @@ -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 +isSafe :: [Integer] -> Bool +isSafe xs = isSortedGradually (<) xs || isSortedGradually (>) xs main :: IO() -main = interact $ show . sum . map (fromEnum . ok . map (read :: (String -> Integer)) . words) . lines +main = interact $ show . sum . map (fromEnum . isSafe . map (read :: (String -> Integer)) . words) . lines