From 5bd3f594c4a1aebee2c2ffcfb6c5bdb0730c6f24 Mon Sep 17 00:00:00 2001 From: Victor Martinez <49537445+JasterV@users.noreply.github.com> Date: Wed, 12 Mar 2025 15:59:45 +0100 Subject: [PATCH] refactor day 2 --- src/Day2.hs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Day2.hs b/src/Day2.hs index 9b6e434..03b0302 100644 --- a/src/Day2.hs +++ b/src/Day2.hs @@ -16,7 +16,6 @@ partOne contents = length . filter isSafeReport <$> parseInput contents partTwo :: String -> Either Error Int partTwo contents = length . filter isSafeReportWithTolerance <$> parseInput contents where - isSafeReportWithTolerance :: Report -> Bool isSafeReportWithTolerance xs = isSafeReport xs || aux [] xs where aux left [] = isSafeReport left @@ -24,17 +23,12 @@ partTwo contents = length . filter isSafeReportWithTolerance <$> parseInput cont isSafeReport :: Report -> Bool isSafeReport xs = isSafeReportWith isSafeIncrease xs || isSafeReportWith isSafeDecrease xs - -isSafeReportWith :: (Level -> Level -> Bool) -> Report -> Bool -isSafeReportWith validator (x : y : xs) = validator x y && isSafeReportWith validator (y : xs) -isSafeReportWith _ [_] = True -isSafeReportWith _ [] = False - -isSafeIncrease :: Level -> Level -> Bool -isSafeIncrease x y = (x - y) > 0 && (x - y) < 4 - -isSafeDecrease :: Level -> Level -> Bool -isSafeDecrease x y = isSafeIncrease y x + where + isSafeReportWith validator (x : y : xs') = validator x y && isSafeReportWith validator (y : xs') + isSafeReportWith _ [_] = True + isSafeReportWith _ [] = False + isSafeIncrease x y = (x - y) > 0 && (x - y) < 4 + isSafeDecrease x y = isSafeIncrease y x parseInput :: String -> Either Error [Report] parseInput contents = mapM parseReport (lines contents)