solve 2022 day 1 part 2
This commit is contained in:
parent
6a79211bec
commit
5408efc536
3 changed files with 19 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ description: Please see the README on GitHub at <https://github.com/sloa
|
|||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
- text
|
||||
|
||||
ghc-options:
|
||||
- -Wall
|
||||
|
|
Loading…
Reference in a new issue