Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace parser combinators with Text.splitOn #27

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Replace parser combinators with Text.splitOn
  • Loading branch information
ephemient committed Dec 3, 2024
commit d67b65d331962f5bb4286902d5e9af1bc0618043
5 changes: 1 addition & 4 deletions hs/aoc2024.cabal
Original file line number Diff line number Diff line change
@@ -31,10 +31,7 @@ library
build-depends:
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,
text ^>=2.1.2,

ghc-options: -Wall
default-language: GHC2024
3 changes: 1 addition & 2 deletions hs/app/Main.hs
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ import Day2 qualified (part1, part2)
import Day3 qualified (part1, part2)
import System.Environment (getArgs, lookupEnv)
import System.FilePath (combine)
import Text.Megaparsec (errorBundlePretty)

getDayInput :: Int -> IO Text
getDayInput i = do
@@ -35,4 +34,4 @@ main :: IO ()
main = do
run 1 (either fail print) [Day1.part1, Day1.part2]
run 2 (either fail print) [Day2.part1, Day2.part2]
run 3 (either (fail . errorBundlePretty) print) [Day3.part1, Day3.part2]
run 3 print [Day3.part1, Day3.part2]
34 changes: 13 additions & 21 deletions hs/src/Day3.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}

-- |
-- Module: Day3
-- Description: <https://adventofcode.com/2024/day/3 Day 3: Mull It Over>
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)
import Data.Void (Void)
import Text.Megaparsec (MonadParsec (try), ParseErrorBundle, Stream (Token, Tokens), anySingle, between, many, parse, skipManyTill, (<|>))
import Text.Megaparsec.Char (char, string)
import Text.Megaparsec.Char.Lexer qualified as L (decimal)
import Data.Text qualified as T (splitOn, pattern (:<))
import Data.Text.Read qualified as T (decimal)

mul :: (MonadParsec e s m, IsString (Tokens s), Token s ~ Char, Num a) => m a
mul = between (string "mul(") (string ")") $ (*) <$> L.decimal <* char ',' <*> L.decimal
part1 :: Text -> Int
part1 input =
sum
[ a * b
| part <- drop 1 $ T.splitOn "mul(" input,
(a, ',' T.:< part') <- rights [T.decimal part],
(b, ')' T.:< _) <- rights [T.decimal part']
]

parser1 :: (MonadParsec e s m, IsString (Tokens s), Token s ~ Char, Num a) => m [a]
parser1 = many $ try $ skipManyTill anySingle $ try mul

parser2 :: (MonadParsec e s m, IsString (Tokens s), Token s ~ Char, Num a) => m [Either Bool a]
parser2 = many $ try $ skipManyTill anySingle $ Right <$> try mul <|> string "do()" $> Left True <|> string "don't()" $> Left False

part1 :: Text -> Either (ParseErrorBundle Text Void) Int
part1 = fmap sum . parse parser1 ""

part2 :: Text -> Either (ParseErrorBundle Text Void) Int
part2 = fmap (sum . rights . flip evalState True . filterM (either (($> False) . put) $ const get)) . parse parser2 ""
part2 :: Text -> Int
part2 input = sum $ part1 <$> (T.splitOn "do()" input >>= take 1 . T.splitOn "don't()")
4 changes: 2 additions & 2 deletions hs/test/Day3Spec.hs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ spec :: Spec
spec = do
describe "part 1" $ do
it "examples" $ do
part1 example1 `shouldBe` Right 161
part1 example1 `shouldBe` 161
describe "part 2" $ do
it "examples" $ do
part2 example2 `shouldBe` Right 48
part2 example2 `shouldBe` 48
Loading