mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-26 18:10:05 +00:00
refactor day 2
This commit is contained in:
parent
fc76286ccd
commit
5bd3f594c4
1 changed files with 6 additions and 12 deletions
18
src/Day2.hs
18
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue