From 45c9a200e21c06912794727da1a29891da848a50 Mon Sep 17 00:00:00 2001 From: kale Date: Tue, 3 Dec 2024 22:17:28 +0100 Subject: [PATCH] feat(day2): Follow the same logic for part 2 --- day2/Main.hs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/day2/Main.hs b/day2/Main.hs index 97a50a7..d49ee10 100644 --- a/day2/Main.hs +++ b/day2/Main.hs @@ -23,19 +23,28 @@ pairUp :: [a] -> [(a, a)] pairUp (a:b:tl) = (a, b) : pairUp (b:tl) pairUp _ = [] -allIncOrDec :: Report -> Bool -allIncOrDec (a:b:tl) - | a /= b = all (uncurry (if a < b then (<) else (>))) $ pairUp $ b:tl - | otherwise = False -allIncOrDec _ = True +allIncOrDec :: Bool -> Report -> Bool +allIncOrDec dampened (a:b:tl) = if a /= b + then (if dampened then (>=) 0 else (==) 0) + $ length + $ filter (not . uncurry (if a < b then (<) else (>))) + $ pairUp + $ b:tl + else dampened && allIncOrDec False (b:tl) +allIncOrDec _ _ = True -notTooDifferent :: Report -> Bool -notTooDifferent (a:b:tl) = let dif = abs (a - b) - in (1 <= dif) && (dif <= 3) && notTooDifferent (b:tl) -notTooDifferent _ = True +notTooDifferent :: Bool -> Report -> Bool +notTooDifferent dampened (a:b:tl) = + let dif = abs (a - b) + in if (1 <= dif) && (dif <= 3) + then notTooDifferent dampened (b:tl) + else dampened && notTooDifferent False (b:tl) +notTooDifferent _ _ = True -part1 :: [Report] -> Int -part1 = length . filter allIncOrDec . filter notTooDifferent +solve :: Bool -> [Report] -> Int +solve dampened = length + . filter (allIncOrDec dampened) + . filter (notTooDifferent dampened) main :: IO () main = do @@ -45,5 +54,5 @@ main = do case parse reports fp content of Left e -> putStrLn ("Input file is incorrectly formatted : " ++ show e) Right r -> do - putStrLn ("Solution (Part 1) : " ++ show (part1 r)) - -- putStrLn ("Solution (Part 2) : " ++ show (part2 r)) + putStrLn ("Solution (Part 1) : " ++ show (solve False r)) + putStrLn ("Solution (Part 2) : " ++ show (solve True r))