adventofcode/2022/app/Day1/Main.hs

33 lines
756 B
Haskell
Raw Normal View History

2022-12-01 22:28:05 +01:00
module Day1.Main (main) where
import Data.List
import System.IO
import Control.Monad
main :: IO ()
main = do
putStrLn "Day 1"
handle <- openFile "app/Day1/input" ReadMode
contents <- hGetContents handle
let r1 = part1 contents
putStrLn $ "part 1: " ++ show r1
let r2 = part2 contents
putStrLn $ "part 2: " ++ show r2
parseInput :: String -> [Int]
parseInput contents = map sum foodItems
where
input = lines contents
elves = foldr (\a b -> if a == "" then [] : b else (a : head b) : tail b) [[]] input
foodItems = (map . map) (read :: String -> Int) elves
part1 :: String -> Int
part1 = maximum . parseInput
part2 :: String -> Int
part2 = sum . (take 3) . reverse . sort . parseInput