mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-27 02:15:43 +00:00
feat: day 2 part 2
This commit is contained in:
parent
69a425e888
commit
fc76286ccd
1 changed files with 20 additions and 11 deletions
33
src/Day2.hs
33
src/Day2.hs
|
|
@ -11,21 +11,30 @@ type Level = Int
|
|||
type Report = [Level]
|
||||
|
||||
partOne :: String -> Either Error Int
|
||||
partOne contents = length . filter isValidReport <$> parseInput contents
|
||||
where
|
||||
isValidReport xs = isValidReportWith isSafeIncrease xs || isValidReportWith isSafeDecrease xs
|
||||
|
||||
isSafeIncrease x y = (x - y) > 0 && (x - y) < 4
|
||||
isSafeDecrease x y = isSafeIncrease y x
|
||||
|
||||
isValidReportWith validator (x : y : xs) = validator x y && isValidReportWith validator (y : xs)
|
||||
isValidReportWith _ [_] = True
|
||||
isValidReportWith _ [] = False
|
||||
partOne contents = length . filter isSafeReport <$> parseInput contents
|
||||
|
||||
partTwo :: String -> Either Error Int
|
||||
partTwo contents = calculateScore <$> parseInput contents
|
||||
partTwo contents = length . filter isSafeReportWithTolerance <$> parseInput contents
|
||||
where
|
||||
calculateScore _ = 0
|
||||
isSafeReportWithTolerance :: Report -> Bool
|
||||
isSafeReportWithTolerance xs = isSafeReport xs || aux [] xs
|
||||
where
|
||||
aux left [] = isSafeReport left
|
||||
aux left (x : xs') = isSafeReport (left ++ xs') || aux (left ++ [x]) xs'
|
||||
|
||||
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
|
||||
|
||||
parseInput :: String -> Either Error [Report]
|
||||
parseInput contents = mapM parseReport (lines contents)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue