diff options
author | Marvin Borner | 2024-12-03 08:55:30 +0100 |
---|---|---|
committer | Marvin Borner | 2024-12-03 08:55:30 +0100 |
commit | 7e41ec6dbccda7fbc483faa5f28a94319abb46b3 (patch) | |
tree | cfcb1e5cd70bd3da04f2b741d02184c30d4a6ef1 /2024/03 | |
parent | a37b1a999421e24ddef1a1931cca8722a076d9a3 (diff) |
overslept
Diffstat (limited to '2024/03')
-rw-r--r-- | 2024/03/solve.hs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/2024/03/solve.hs b/2024/03/solve.hs new file mode 100644 index 0000000..38e143e --- /dev/null +++ b/2024/03/solve.hs @@ -0,0 +1,53 @@ +-- I had expected a more complex syntax in p2 :) + +import Data.Functor +import Data.Void +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L + +type Parser = Parsec Void String +type Enabled = Bool + +data Instr = Mul Int Int | Do | Dont | Garbage + deriving Show + +multiplication :: Parser Instr +multiplication = do + string "mul(" + a <- L.decimal + string "," + b <- L.decimal + string ")" + pure $ Mul a b + +program :: Parser [Instr] +program = + many + $ try multiplication + <|> (string "don't()" $> Dont) + <|> (string "do()" $> Do) + <|> (satisfy (const True) $> Garbage) + + +part1 :: [Instr] -> Int +part1 = sum . map eval + where + eval (Mul a b) = a * b + eval _ = 0 + +part2 :: Enabled -> [Instr] -> Int +part2 True (Mul a b : is) = a * b + part2 True is +part2 True (Dont : is) = part2 False is +part2 False (Do : is) = part2 True is +part2 e (_ : is) = part2 e is +part2 _ [] = 0 + +main :: IO () +main = do + f <- readFile "input" + case runParser (program <* eof) "" f of + Right p -> do + print $ part1 p + print $ part2 True p + Left err -> print $ errorBundlePretty err |