Cleanup day 1
This commit is contained in:
@@ -30,10 +30,10 @@ day1_2 = do
|
|||||||
|
|
||||||
findCaloriesOfTop3Elves :: T.Text -> CalorieCount
|
findCaloriesOfTop3Elves :: T.Text -> CalorieCount
|
||||||
findCaloriesOfTop3Elves =
|
findCaloriesOfTop3Elves =
|
||||||
calories . sumCalories . take 3 . sortElves . elfToCalories
|
calories . sumCalories . take 3 . sortElves . parseElfInput
|
||||||
where
|
where
|
||||||
sumCalories :: [Elf] -> Int
|
sumCalories :: [Elf] -> Int
|
||||||
sumCalories = sum . map caloriesOf
|
sumCalories = sum . map getCalories
|
||||||
|
|
||||||
findElfWithHighestCalorieCount :: T.Text -> Elf
|
findElfWithHighestCalorieCount :: T.Text -> Elf
|
||||||
findElfWithHighestCalorieCount = head .sortElves . elfToCalories
|
findElfWithHighestCalorieCount = head . sortElves . parseElfInput
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
module Day1.Internal (
|
module Day1.Internal (
|
||||||
elfToCalories,
|
parseElfInput,
|
||||||
Elf,
|
Elf,
|
||||||
CalorieCount,
|
CalorieCount,
|
||||||
calories,
|
calories,
|
||||||
@@ -8,7 +8,7 @@ module Day1.Internal (
|
|||||||
parseElfGroups,
|
parseElfGroups,
|
||||||
intOrZero,
|
intOrZero,
|
||||||
sortElves,
|
sortElves,
|
||||||
caloriesOf
|
getCalories
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
@@ -41,17 +41,14 @@ elf c = Elf (CalorieCount c)
|
|||||||
calories :: Int -> CalorieCount
|
calories :: Int -> CalorieCount
|
||||||
calories = CalorieCount
|
calories = CalorieCount
|
||||||
|
|
||||||
caloriesOf :: Elf -> Int
|
getCalories :: Elf -> Int
|
||||||
caloriesOf (Elf (CalorieCount c)) = c
|
getCalories (Elf (CalorieCount c)) = c
|
||||||
|
|
||||||
sortElves :: [Elf] -> [Elf]
|
sortElves :: [Elf] -> [Elf]
|
||||||
sortElves = L.reverse . L.sort
|
sortElves = L.reverse . L.sort
|
||||||
|
|
||||||
elfToCalories :: T.Text -> [Elf]
|
parseElfInput :: T.Text -> [Elf]
|
||||||
elfToCalories input = map elf sums
|
parseElfInput input = map (elf . sum) (parseElfGroups input)
|
||||||
where
|
|
||||||
groups = parseElfGroups input
|
|
||||||
sums = map sum groups
|
|
||||||
|
|
||||||
parseElfGroups :: T.Text -> [[Int]]
|
parseElfGroups :: T.Text -> [[Int]]
|
||||||
parseElfGroups input = map (map intOrZero) groups
|
parseElfGroups input = map (map intOrZero) groups
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ main = hspec $ do
|
|||||||
sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000]
|
sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000]
|
||||||
it "converts a list of strings to a list of either int or caloriecount" $ do
|
it "converts a list of strings to a list of either int or caloriecount" $ do
|
||||||
input <- readFile "ressources/day01-input-test"
|
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 ]
|
[ elf 6000, elf 4000, elf 11000, elf 24000, elf 10000 ]
|
||||||
it "parser text to int" $ do
|
it "parser text to int" $ do
|
||||||
intOrZero "1000" `shouldBe` 1000
|
intOrZero "1000" `shouldBe` 1000
|
||||||
|
|||||||
Reference in New Issue
Block a user