mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-26 18:10:05 +00:00
refactor: prepare Day1 to be tested
This commit is contained in:
parent
14ba05fc6c
commit
a36b2e1ce5
1 changed files with 5 additions and 16 deletions
21
src/Day1.hs
21
src/Day1.hs
|
|
@ -4,7 +4,6 @@ module Day1
|
|||
)
|
||||
where
|
||||
|
||||
import Control.Exception (try)
|
||||
import qualified Data.Bifunctor as BF
|
||||
import Data.Either
|
||||
import Data.List (sort)
|
||||
|
|
@ -18,17 +17,11 @@ data ParseError
|
|||
| ParseLineError String
|
||||
deriving (Show)
|
||||
|
||||
data Error
|
||||
= ReadFileError FilePath IOError
|
||||
| ParseInputError ParseError
|
||||
newtype Error = ParseInputError ParseError
|
||||
deriving (Show)
|
||||
|
||||
partOne :: String -> IO (Either Error Int)
|
||||
partOne filepath = do
|
||||
result <- try (readFile filepath)
|
||||
case result of
|
||||
Left readError -> return $ Left (ReadFileError filepath readError)
|
||||
Right contents -> return $ calculateScore <$> BF.first ParseInputError (parseInput contents)
|
||||
partOne :: String -> Either Error Int
|
||||
partOne contents = calculateScore <$> BF.first ParseInputError (parseInput contents)
|
||||
where
|
||||
-- To calculate the overall score we just need to
|
||||
-- sort the lists and calculate the distances between each element
|
||||
|
|
@ -36,12 +29,8 @@ partOne filepath = do
|
|||
sum $ zipWith distance (sort left) (sort right)
|
||||
distance a b = abs (a - b)
|
||||
|
||||
partTwo :: String -> IO (Either Error Int)
|
||||
partTwo filepath = do
|
||||
result <- try (readFile filepath)
|
||||
case result of
|
||||
Left readError -> return $ Left (ReadFileError filepath readError)
|
||||
Right contents -> return $ calculateScore <$> BF.first ParseInputError (parseInput contents)
|
||||
partTwo :: String -> Either Error Int
|
||||
partTwo contents = calculateScore <$> BF.first ParseInputError (parseInput contents)
|
||||
where
|
||||
-- To calculate the overall score we need to sum all the similarity scores
|
||||
-- of each element from the left list applied to the right list
|
||||
|
|
|
|||
Loading…
Reference in a new issue