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
|
where
|
||||||
|
|
||||||
import Control.Exception (try)
|
|
||||||
import qualified Data.Bifunctor as BF
|
import qualified Data.Bifunctor as BF
|
||||||
import Data.Either
|
import Data.Either
|
||||||
import Data.List (sort)
|
import Data.List (sort)
|
||||||
|
|
@ -18,17 +17,11 @@ data ParseError
|
||||||
| ParseLineError String
|
| ParseLineError String
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
data Error
|
newtype Error = ParseInputError ParseError
|
||||||
= ReadFileError FilePath IOError
|
|
||||||
| ParseInputError ParseError
|
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
partOne :: String -> IO (Either Error Int)
|
partOne :: String -> Either Error Int
|
||||||
partOne filepath = do
|
partOne contents = calculateScore <$> BF.first ParseInputError (parseInput contents)
|
||||||
result <- try (readFile filepath)
|
|
||||||
case result of
|
|
||||||
Left readError -> return $ Left (ReadFileError filepath readError)
|
|
||||||
Right contents -> return $ calculateScore <$> BF.first ParseInputError (parseInput contents)
|
|
||||||
where
|
where
|
||||||
-- To calculate the overall score we just need to
|
-- To calculate the overall score we just need to
|
||||||
-- sort the lists and calculate the distances between each element
|
-- sort the lists and calculate the distances between each element
|
||||||
|
|
@ -36,12 +29,8 @@ partOne filepath = do
|
||||||
sum $ zipWith distance (sort left) (sort right)
|
sum $ zipWith distance (sort left) (sort right)
|
||||||
distance a b = abs (a - b)
|
distance a b = abs (a - b)
|
||||||
|
|
||||||
partTwo :: String -> IO (Either Error Int)
|
partTwo :: String -> Either Error Int
|
||||||
partTwo filepath = do
|
partTwo contents = calculateScore <$> BF.first ParseInputError (parseInput contents)
|
||||||
result <- try (readFile filepath)
|
|
||||||
case result of
|
|
||||||
Left readError -> return $ Left (ReadFileError filepath readError)
|
|
||||||
Right contents -> return $ calculateScore <$> BF.first ParseInputError (parseInput contents)
|
|
||||||
where
|
where
|
||||||
-- To calculate the overall score we need to sum all the similarity scores
|
-- 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
|
-- of each element from the left list applied to the right list
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue