diff --git a/day2/Main.hs b/day2/Main.hs index 90b8650..97a50a7 100644 --- a/day2/Main.hs +++ b/day2/Main.hs @@ -2,8 +2,6 @@ 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) @@ -25,28 +23,19 @@ pairUp :: [a] -> [(a, a)] pairUp (a:b:tl) = (a, b) : pairUp (b:tl) pairUp _ = [] -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 - $ a:b:tl) -isSafe _ = True +allIncOrDec :: Report -> Bool +allIncOrDec (a:b:tl) + | a /= b = all (uncurry (if a < b then (<) else (>))) $ pairUp $ b:tl + | otherwise = False +allIncOrDec _ = True -with1Removed :: Report -> [Report] -with1Removed (h:tl) = tl : map (h :) (with1Removed tl) -with1Removed [] = [] - -isKindaSafe :: Report -> Bool -isKindaSafe = any isSafe . with1Removed +notTooDifferent :: Report -> Bool +notTooDifferent (a:b:tl) = let dif = abs (a - b) + in (1 <= dif) && (dif <= 3) && notTooDifferent (b:tl) +notTooDifferent _ = True part1 :: [Report] -> Int -part1 = length . filter isSafe - -part2 :: [Report] -> Int -part2 = length . filter isKindaSafe +part1 = length . filter allIncOrDec . filter notTooDifferent main :: IO () main = do @@ -57,4 +46,4 @@ main = do 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 2) : " ++ show (part2 r))