-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ephemient/hs/day3
- Loading branch information
Showing
7 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day3 | ||
-- Description: <https://adventofcode.com/2024/day/3 Day 3: Mull It Over> | ||
module Day3 (part1, part2) where | ||
|
||
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) | ||
|
||
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 | ||
|
||
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 (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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day3Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day3 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example1, example2 :: Text | ||
example1 = | ||
T.unlines | ||
[ "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))" | ||
] | ||
example2 = | ||
T.unlines | ||
[ "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example1 `shouldBe` Right 161 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example2 `shouldBe` Right 48 |