diff --git a/src/Day1.hs b/src/Day1.hs index dbb89d6..cd608ee 100644 --- a/src/Day1.hs +++ b/src/Day1.hs @@ -30,10 +30,10 @@ day1_2 = do findCaloriesOfTop3Elves :: T.Text -> CalorieCount findCaloriesOfTop3Elves = - calories . sumCalories . take 3 . sortElves . elfToCalories + calories . sumCalories . take 3 . sortElves . parseElfInput where sumCalories :: [Elf] -> Int - sumCalories = sum . map caloriesOf + sumCalories = sum . map getCalories findElfWithHighestCalorieCount :: T.Text -> Elf -findElfWithHighestCalorieCount = head .sortElves . elfToCalories +findElfWithHighestCalorieCount = head . sortElves . parseElfInput diff --git a/src/Day1/Internal.hs b/src/Day1/Internal.hs index 33d9846..208f76d 100644 --- a/src/Day1/Internal.hs +++ b/src/Day1/Internal.hs @@ -1,6 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} module Day1.Internal ( - elfToCalories, + parseElfInput, Elf, CalorieCount, calories, @@ -8,7 +8,7 @@ module Day1.Internal ( parseElfGroups, intOrZero, sortElves, - caloriesOf + getCalories ) where import qualified Data.Text as T @@ -25,7 +25,7 @@ instance Show CalorieCount where instance Show Elf where show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]" - + instance Eq CalorieCount where (==) (CalorieCount c1) (CalorieCount c2) = c1 == c2 @@ -41,17 +41,14 @@ elf c = Elf (CalorieCount c) calories :: Int -> CalorieCount calories = CalorieCount -caloriesOf :: Elf -> Int -caloriesOf (Elf (CalorieCount c)) = c +getCalories :: Elf -> Int +getCalories (Elf (CalorieCount c)) = c sortElves :: [Elf] -> [Elf] sortElves = L.reverse . L.sort -elfToCalories :: T.Text -> [Elf] -elfToCalories input = map elf sums - where - groups = parseElfGroups input - sums = map sum groups +parseElfInput :: T.Text -> [Elf] +parseElfInput input = map (elf . sum) (parseElfGroups input) parseElfGroups :: T.Text -> [[Int]] parseElfGroups input = map (map intOrZero) groups diff --git a/test/Spec.hs b/test/Spec.hs index 9e21a77..3955677 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -12,7 +12,7 @@ main = hspec $ do sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000] it "converts a list of strings to a list of either int or caloriecount" $ do input <- readFile "ressources/day01-input-test" - elfToCalories (T.pack input) `shouldBe` + parseElfInput (T.pack input) `shouldBe` [ elf 6000, elf 4000, elf 11000, elf 24000, elf 10000 ] it "parser text to int" $ do intOrZero "1000" `shouldBe` 1000