From 45c9a200e21c06912794727da1a29891da848a50 Mon Sep 17 00:00:00 2001 From: kale Date: Tue, 3 Dec 2024 22:17:28 +0100 Subject: [PATCH 1/2] 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)) From 71c6797d8145d03b60d8791430a13b3616a4682b Mon Sep 17 00:00:00 2001 From: kale Date: Wed, 4 Dec 2024 00:04:53 +0100 Subject: [PATCH 2/2] feat(day2): Fuck it we ball --- day2/Main.hs | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/day2/Main.hs b/day2/Main.hs index d49ee10..90b8650 100644 --- a/day2/Main.hs +++ b/day2/Main.hs @@ -2,6 +2,8 @@ module Main where import System.Environment (getArgs) +import Control.Exception (assert) + import Text.Parsec (parse, many1, char, sepBy, endBy) import Text.Parsec.Char (endOfLine) import Text.ParserCombinators.Parsec (GenParser) @@ -23,28 +25,28 @@ pairUp :: [a] -> [(a, a)] pairUp (a:b:tl) = (a, b) : pairUp (b:tl) pairUp _ = [] -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 (>))) +isSafe :: Report -> Bool +isSafe (a:b:tl) = a /= b && (==) 0 ( + length $ filter (not . (\(l, r) -> let + dif = abs (l - r) + cmp = if a < b then (<) else assert (b < a) (>) + in (1 <= dif) && (dif <= 3) && l `cmp` r)) $ pairUp - $ b:tl - else dampened && allIncOrDec False (b:tl) -allIncOrDec _ _ = True + $ a:b:tl) +isSafe _ = 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 +with1Removed :: Report -> [Report] +with1Removed (h:tl) = tl : map (h :) (with1Removed tl) +with1Removed [] = [] -solve :: Bool -> [Report] -> Int -solve dampened = length - . filter (allIncOrDec dampened) - . filter (notTooDifferent dampened) +isKindaSafe :: Report -> Bool +isKindaSafe = any isSafe . with1Removed + +part1 :: [Report] -> Int +part1 = length . filter isSafe + +part2 :: [Report] -> Int +part2 = length . filter isKindaSafe main :: IO () main = do @@ -54,5 +56,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 (solve False r)) - putStrLn ("Solution (Part 2) : " ++ show (solve True r)) + putStrLn ("Solution (Part 1) : " ++ show (part1 r)) + putStrLn ("Solution (Part 2) : " ++ show (part2 r))