day1
This commit is contained in:
39
src/Day1.hs
Normal file
39
src/Day1.hs
Normal file
@@ -0,0 +1,39 @@
|
||||
module Day1 (
|
||||
day1,
|
||||
findElfWithHighestCalorieCount,
|
||||
findCaloriesOfTop3Elves
|
||||
) where
|
||||
|
||||
import qualified Data.Text as T
|
||||
import Day1.Internal
|
||||
|
||||
day1 :: IO ()
|
||||
day1 = do
|
||||
putStrLn "Day1"
|
||||
calorieCount <- day1_1
|
||||
putStrLn ("Highest calorie count = " ++ calorieCount)
|
||||
caloriesOfTop3Elves <- day1_2
|
||||
putStrLn ("Calories of top 3 elves = " ++ caloriesOfTop3Elves)
|
||||
putStrLn "-----------"
|
||||
|
||||
|
||||
day1_1 :: IO String
|
||||
day1_1 = do
|
||||
input <- readFile "ressources/day01-input"
|
||||
return (show $ findElfWithHighestCalorieCount $ T.pack input)
|
||||
|
||||
day1_2 :: IO String
|
||||
day1_2 = do
|
||||
input <- readFile "ressources/day01-input"
|
||||
return (show $ findCaloriesOfTop3Elves $ T.pack input)
|
||||
|
||||
|
||||
findCaloriesOfTop3Elves :: T.Text -> CalorieCount
|
||||
findCaloriesOfTop3Elves =
|
||||
calories . sumCalories . take 3 . sortElves . elfToCalories
|
||||
where
|
||||
sumCalories :: [Elf] -> Int
|
||||
sumCalories = sum . map caloriesOf
|
||||
|
||||
findElfWithHighestCalorieCount :: T.Text -> Elf
|
||||
findElfWithHighestCalorieCount = head .sortElves . elfToCalories
|
||||
66
src/Day1/Internal.hs
Normal file
66
src/Day1/Internal.hs
Normal file
@@ -0,0 +1,66 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
module Day1.Internal (
|
||||
elfToCalories,
|
||||
Elf,
|
||||
CalorieCount,
|
||||
calories,
|
||||
elf,
|
||||
parseElfGroups,
|
||||
intOrZero,
|
||||
sortElves,
|
||||
caloriesOf
|
||||
) where
|
||||
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Read as R
|
||||
import Data.List.Split
|
||||
import qualified Data.List as L
|
||||
import Data.Either
|
||||
|
||||
newtype CalorieCount = CalorieCount Int
|
||||
newtype Elf = Elf CalorieCount
|
||||
|
||||
instance Show CalorieCount where
|
||||
show (CalorieCount c) = "Calories[" ++ show c ++ "]"
|
||||
|
||||
instance Show Elf where
|
||||
show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]"
|
||||
|
||||
instance Eq CalorieCount where
|
||||
(==) (CalorieCount c1) (CalorieCount c2) = c1 == c2
|
||||
|
||||
instance Eq Elf where
|
||||
(==) (Elf (CalorieCount c1)) (Elf (CalorieCount c2)) = c1 == c2
|
||||
|
||||
instance Ord Elf where
|
||||
compare (Elf (CalorieCount c1)) (Elf (CalorieCount c2)) = compare c1 c2
|
||||
|
||||
elf :: Int -> Elf
|
||||
elf c = Elf (CalorieCount c)
|
||||
|
||||
calories :: Int -> CalorieCount
|
||||
calories = CalorieCount
|
||||
|
||||
caloriesOf :: Elf -> Int
|
||||
caloriesOf (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
|
||||
|
||||
parseElfGroups :: T.Text -> [[Int]]
|
||||
parseElfGroups input = map (map intOrZero) groups
|
||||
where
|
||||
groups = splitWhen (== "") $ T.lines input
|
||||
|
||||
|
||||
intOrZero:: T.Text -> Int
|
||||
intOrZero text = val
|
||||
where
|
||||
(val, _) = fromRight (0, "") $ R.decimal text
|
||||
|
||||
9
src/Lib.hs
Normal file
9
src/Lib.hs
Normal file
@@ -0,0 +1,9 @@
|
||||
module Lib
|
||||
( someFunc
|
||||
) where
|
||||
|
||||
import Day1
|
||||
|
||||
someFunc :: IO ()
|
||||
someFunc = day1
|
||||
|
||||
Reference in New Issue
Block a user