mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-26 18:10:05 +00:00
feat: day 1 part one
This commit is contained in:
parent
3035985d6b
commit
69a425e888
2 changed files with 1024 additions and 5 deletions
1000
input/day2.txt
Normal file
1000
input/day2.txt
Normal file
File diff suppressed because it is too large
Load diff
29
src/Day2.hs
29
src/Day2.hs
|
|
@ -1,18 +1,37 @@
|
|||
module Day2 (partOne, partTwo) where
|
||||
|
||||
data Error = ParseError String
|
||||
import qualified Data.Bifunctor as BF
|
||||
import Text.Read (readEither)
|
||||
|
||||
newtype Error = ParseError String
|
||||
deriving (Show)
|
||||
|
||||
type Level = Int
|
||||
|
||||
type Report = [Level]
|
||||
|
||||
partOne :: String -> Either Error Int
|
||||
partOne contents = calculateScore <$> parseInput contents
|
||||
partOne contents = length . filter isValidReport <$> parseInput contents
|
||||
where
|
||||
calculateScore _ = 0
|
||||
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
|
||||
|
||||
partTwo :: String -> Either Error Int
|
||||
partTwo contents = calculateScore <$> parseInput contents
|
||||
where
|
||||
calculateScore _ = 0
|
||||
|
||||
parseInput :: String -> Either Error [Int]
|
||||
parseInput _ = Right []
|
||||
parseInput :: String -> Either Error [Report]
|
||||
parseInput contents = mapM parseReport (lines contents)
|
||||
where
|
||||
parseReport :: String -> Either Error Report
|
||||
parseReport line = mapM parseLevel (words line)
|
||||
|
||||
parseLevel :: String -> Either Error Level
|
||||
parseLevel word = BF.first (const $ ParseError word) (readEither word :: Either String Level)
|
||||
|
|
|
|||
Loading…
Reference in a new issue