51 lines
1.3 KiB
Haskell
51 lines
1.3 KiB
Haskell
{-# LANGUAGE LambdaCase #-}
|
|
module Main where
|
|
|
|
import Data.List as L
|
|
import Data.Maybe
|
|
import Safe
|
|
|
|
data InputOptions = Elf1
|
|
| Elf3
|
|
| Quit
|
|
|
|
|
|
getInput :: IO InputOptions
|
|
getInput = do
|
|
putStrLn "Only get top Elf or sum of top 3 Elf? (1/3/q)"
|
|
getLine >>= \case
|
|
"1" -> return Elf1
|
|
"3" -> return Elf3
|
|
"q" -> return Quit
|
|
_ -> putStrLn "not understood" >> getInput
|
|
main :: IO ()
|
|
main = getInput >>= \case
|
|
Quit -> putStrLn "bye!"
|
|
Elf1 -> interact $
|
|
(<>"\n")
|
|
. show
|
|
. maximum
|
|
. fmap (sum . mapMaybe (readMay :: String -> Maybe Integer))
|
|
. splitOn ""
|
|
. lines
|
|
Elf3 -> interact $
|
|
(<>"\n")
|
|
. show
|
|
. sum
|
|
. take 3
|
|
. sortOn negate
|
|
. fmap (sum . mapMaybe (readMay :: String -> Maybe Integer))
|
|
. splitOn ""
|
|
. lines
|
|
|
|
|
|
-- | Split a list into sublists delimited by the given element.
|
|
--
|
|
-- From: https://hackage.haskell.org/package/haskell-gi-0.26.2/docs/src/Data.GI.CodeGen.Util.html#splitOn
|
|
splitOn :: Eq a => a -> [a] -> [[a]]
|
|
splitOn x xs = go xs []
|
|
where go [] acc = [reverse acc]
|
|
go (y : ys) acc = if x == y
|
|
then reverse acc : go ys []
|
|
else go ys (y : acc)
|