feat(day2): Follow the same logic for part 2

This commit is contained in:
kale 2024-12-03 22:17:28 +01:00
parent cb83e4247d
commit 45c9a200e2
Signed by: kalmenn
GPG key ID: F500055C44BC3834

View file

@ -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))