aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
authorJulien Dessaux2023-04-09 00:17:58 +0200
committerJulien Dessaux2023-04-09 00:17:58 +0200
commitfe2d95c8fe32053a528e164da1253f00f776afbb (patch)
tree6f49d682d4d093a221ef8ffeaaf8f1d75bad3daa /2020
parent2020-18 part 1 in haskell (diff)
downloadadvent-of-code-fe2d95c8fe32053a528e164da1253f00f776afbb.tar.gz
advent-of-code-fe2d95c8fe32053a528e164da1253f00f776afbb.tar.bz2
advent-of-code-fe2d95c8fe32053a528e164da1253f00f776afbb.zip
2020-18 part 2 in haskell
Diffstat (limited to '')
-rw-r--r--2020/18-Operation_Order/second.hs80
1 files changed, 80 insertions, 0 deletions
diff --git a/2020/18-Operation_Order/second.hs b/2020/18-Operation_Order/second.hs
new file mode 100644
index 0000000..01a31a5
--- /dev/null
+++ b/2020/18-Operation_Order/second.hs
@@ -0,0 +1,80 @@
+-- requires cabal install --lib megaparsec parser-combinators
+module Main (main) where
+import Control.Monad (void, when)
+import Data.Either
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import System.Exit (die)
+
+exampleExpectedOutput = 231 + 51
+
+data Operation = Add | Mul deriving (Eq, Show)
+data Chain = Chain [Either Int Chain] [Operation] deriving Show
+type Input = [Chain]
+
+type Parser = Parsec Void String
+
+parseInt :: Parser Int
+parseInt = do
+ n <- some digitChar
+ void $ optional (char ' ')
+ return $ read n
+
+parseOp :: Parser Operation
+parseOp = do
+ op <- (char '+' *> return Add) <|> (char '*' *> return Mul)
+ void $ char ' '
+ return op
+
+parseParenthesis :: Parser Chain
+parseParenthesis = do
+ void $ char '('
+ a <- parseChain
+ void $ char ')'
+ void $ optional (char ' ')
+ return $ a
+
+parseChain :: Parser Chain
+parseChain = do
+ a <- ((Left <$> parseInt) <|> (Right <$> parseParenthesis))
+ next <- (Just <$> parseOp) <|> (return Nothing)
+ case next of
+ Nothing -> return $ Chain [a] []
+ Just op -> do
+ Chain elems ops <- parseChain
+ return $ Chain (a:elems) (op:ops)
+
+parseInput' :: Parser Input
+parseInput' = do
+ input <- some (parseChain <* void (optional (char '\n')))
+ void eof
+ return $ input
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseInput' filename input of
+ Left bundle -> die $ errorBundlePretty bundle
+ Right input' -> return input'
+
+compute' :: Chain -> Int
+compute' (Chain [Left x] []) = x
+compute' (Chain [Right x] []) = compute' x
+compute' (Chain (x:y:xs) (op:ops))
+ | op == Add = compute' $ Chain (Left (cx+cy):xs) ops
+ | op == Mul = cx * (compute' $ Chain (Left cy:xs) ops)
+ where
+ cx = if isLeft x then fromLeft 0 x else compute' (fromRight (Chain [] []) x)
+ cy = if isLeft y then fromLeft 0 y else compute' (fromRight (Chain [] []) y)
+
+compute :: Input -> Int
+compute input = sum $ map compute' input
+
+main :: IO ()
+main = do
+ example <- parseInput "example"
+ let exampleOutput = compute example
+ when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
+ input <- parseInput "input"
+ print $ compute input