2022-12-01 08:57:41 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2022-12-01 08:29:30 -05:00
|
|
|
module Main (main) where
|
2022-12-01 08:57:41 -05:00
|
|
|
|
2022-12-01 09:26:39 -05:00
|
|
|
import System.Environment (getArgs)
|
|
|
|
import Data.Ord
|
|
|
|
import Data.List (sortBy)
|
|
|
|
import Data.Text (pack, splitOn, unpack)
|
2022-12-01 08:29:30 -05:00
|
|
|
|
2022-12-01 09:26:39 -05:00
|
|
|
parseInput :: String -> [[Integer]]
|
|
|
|
parseInput = (map (map read . words . unpack)) . splitOn "\n\n" . pack
|
2022-12-01 08:29:30 -05:00
|
|
|
|
2022-12-01 09:26:39 -05:00
|
|
|
inputSums :: String -> [Integer]
|
|
|
|
inputSums = map sum . parseInput
|
2022-12-01 08:29:30 -05:00
|
|
|
|
2022-12-01 09:26:39 -05:00
|
|
|
part1 :: String -> Integer
|
|
|
|
part1 = maximum . inputSums
|
2022-12-01 08:57:41 -05:00
|
|
|
|
|
|
|
part2 :: String -> Integer
|
2022-12-01 09:26:39 -05:00
|
|
|
part2 = sum . take 3 . sortDesc . inputSums
|
|
|
|
where
|
|
|
|
sortDesc = sortBy (comparing Down)
|
2022-12-01 08:57:41 -05:00
|
|
|
|
2022-12-01 08:29:30 -05:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2022-12-01 09:26:39 -05:00
|
|
|
args <- getArgs
|
2022-12-01 08:29:30 -05:00
|
|
|
contents <- getContents
|
|
|
|
|
2022-12-01 09:26:39 -05:00
|
|
|
let f = case args of ("1":_) -> part1
|
|
|
|
("2":_) -> part2
|
|
|
|
_ -> error "unknown part"
|
|
|
|
|
|
|
|
putStrLn $ show $ f contents
|
2022-12-01 08:29:30 -05:00
|
|
|
|