53 lines
740 B
Haskell
53 lines
740 B
Haskell
import System.IO
|
|
import Data.List
|
|
import Data.List.Split
|
|
|
|
|
|
|
|
|
|
toNum' :: String -> Int
|
|
toNum' "" = -1
|
|
toNum' s = read s :: Int
|
|
|
|
toNum :: [String] -> [Int]
|
|
toNum = map toNum'
|
|
|
|
|
|
groups :: [Int] -> [[Int]]
|
|
groups = splitOn [-1]
|
|
|
|
|
|
parseElves :: String -> [[Int]]
|
|
parseElves = groups . toNum . lines
|
|
|
|
|
|
elveTotals :: [[Int]] -> [Int]
|
|
elveTotals = map sum
|
|
|
|
|
|
|
|
|
|
topElf :: [[Int]] -> Int
|
|
topElf = maximum . elveTotals
|
|
|
|
|
|
|
|
|
|
sortDesc :: Ord a => [a] -> [a]
|
|
sortDesc = sortBy (flip compare)
|
|
|
|
|
|
top3Elves :: [[Int]] -> Int
|
|
top3Elves = sum . take 3 . sortDesc . elveTotals
|
|
|
|
|
|
|
|
|
|
handler :: String -> String
|
|
handler s = (show $ topElf $ parseElves s) ++ "\n" ++
|
|
(show $ top3Elves $ parseElves s) ++ "\n"
|
|
|
|
main :: IO ()
|
|
main = do
|
|
interact handler
|