diff --git a/hs/aoc2024.cabal b/hs/aoc2024.cabal index f77b94ea..1bc53a88 100644 --- a/hs/aoc2024.cabal +++ b/hs/aoc2024.cabal @@ -32,6 +32,7 @@ library base ^>=4.20.0.0, containers ^>=0.7, megaparsec ^>=9.7.0, + mtl ^>=2.3.1, parser-combinators ^>=1.3.0, text ^>=2.1.1, @@ -63,6 +64,7 @@ test-suite aoc2024-test other-modules: Day1Spec Day2Spec + Day3Spec build-depends: aoc2024, diff --git a/hs/src/Day3.hs b/hs/src/Day3.hs index 367790a1..af9034d8 100644 --- a/hs/src/Day3.hs +++ b/hs/src/Day3.hs @@ -1,10 +1,11 @@ -{-# LANGUAGE OverloadedStrings #-} - -- | -- Module: Day3 -- Description: module Day3 (part1, part2) where +import Control.Monad (filterM) +import Control.Monad.State (MonadState (get, put), evalState) +import Data.Either (rights) import Data.Functor (($>)) import Data.String (IsString) import Data.Text (Text) @@ -26,8 +27,4 @@ part1 :: Text -> Either (ParseErrorBundle Text Void) Int part1 = fmap sum . parse parser1 "" part2 :: Text -> Either (ParseErrorBundle Text Void) Int -part2 = fmap (snd . foldl' go (True, 0)) . parse parser2 "" - where - go (_, a) (Left z) = (z, a) - go (True, a) (Right b) = (True,) $! a + b - go k _ = k +part2 = fmap (sum . rights . flip evalState True . filterM (either (($> False) . put) $ const get)) . parse parser2 ""