Cleanup day 1
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user