From 5408efc53628e16a65161dbe18e46bbfb20b5394 Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Thu, 1 Dec 2022 08:57:41 -0500 Subject: [PATCH] solve 2022 day 1 part 2 --- 2022/aoc.cabal | 3 +++ 2022/app/Main.hs | 16 +++++++++++++++- 2022/package.yaml | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/2022/aoc.cabal b/2022/aoc.cabal index e130fc6..f8eafda 100644 --- a/2022/aoc.cabal +++ b/2022/aoc.cabal @@ -33,6 +33,7 @@ library ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints build-depends: base >=4.7 && <5 + , text default-language: Haskell2010 executable aoc-exe @@ -45,6 +46,7 @@ executable aoc-exe build-depends: aoc , base >=4.7 && <5 + , text default-language: Haskell2010 test-suite aoc-test @@ -58,4 +60,5 @@ test-suite aoc-test build-depends: aoc , base >=4.7 && <5 + , text default-language: Haskell2010 diff --git a/2022/app/Main.hs b/2022/app/Main.hs index 380f86b..3897712 100644 --- a/2022/app/Main.hs +++ b/2022/app/Main.hs @@ -1,4 +1,8 @@ +{-# LANGUAGE OverloadedStrings #-} module Main (main) where + +import Data.List (sort) +import Data.Text (Text, pack, splitOn, unpack) part1 :: String -> Integer @@ -9,9 +13,19 @@ part1 str = findMax lines' [] 0 findMax ("":xs) acc cMax = findMax xs [] $ max (sum acc) cMax findMax (x:xs) acc cMax = findMax xs ((read x):acc) cMax +chunkElves :: Text -> [Integer] +chunkElves t = integers + where + textChunks = splitOn "\n\n" t + integerChunks = map (map read . words . unpack) textChunks + integers = map sum integerChunks + +part2 :: String -> Integer +part2 str = sum $ take 3 $ reverse $ sort $ chunkElves $ pack str + main :: IO () main = do contents <- getContents - putStrLn $ show $ part1 contents + putStrLn $ show $ part2 contents diff --git a/2022/package.yaml b/2022/package.yaml index a26f7e5..c85a109 100644 --- a/2022/package.yaml +++ b/2022/package.yaml @@ -21,6 +21,7 @@ description: Please see the README on GitHub at = 4.7 && < 5 +- text ghc-options: - -Wall