1
0
Fork 0

solve 2022 day 1 part 2

This commit is contained in:
Sloane Perrault 2022-12-01 08:57:41 -05:00
parent 6a79211bec
commit 5408efc536
3 changed files with 19 additions and 1 deletions

View file

@ -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 ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends: build-depends:
base >=4.7 && <5 base >=4.7 && <5
, text
default-language: Haskell2010 default-language: Haskell2010
executable aoc-exe executable aoc-exe
@ -45,6 +46,7 @@ executable aoc-exe
build-depends: build-depends:
aoc aoc
, base >=4.7 && <5 , base >=4.7 && <5
, text
default-language: Haskell2010 default-language: Haskell2010
test-suite aoc-test test-suite aoc-test
@ -58,4 +60,5 @@ test-suite aoc-test
build-depends: build-depends:
aoc aoc
, base >=4.7 && <5 , base >=4.7 && <5
, text
default-language: Haskell2010 default-language: Haskell2010

View file

@ -1,4 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where module Main (main) where
import Data.List (sort)
import Data.Text (Text, pack, splitOn, unpack)
part1 :: String -> Integer part1 :: String -> Integer
@ -9,9 +13,19 @@ part1 str = findMax lines' [] 0
findMax ("":xs) acc cMax = findMax xs [] $ max (sum acc) cMax findMax ("":xs) acc cMax = findMax xs [] $ max (sum acc) cMax
findMax (x:xs) acc cMax = findMax xs ((read x):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 :: IO ()
main = do main = do
contents <- getContents contents <- getContents
putStrLn $ show $ part1 contents putStrLn $ show $ part2 contents

View file

@ -21,6 +21,7 @@ description: Please see the README on GitHub at <https://github.com/sloa
dependencies: dependencies:
- base >= 4.7 && < 5 - base >= 4.7 && < 5
- text
ghc-options: ghc-options:
- -Wall - -Wall