From c29e1480011138ece936a844b4bf3e658b61590d Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 18 Dec 2020 16:16:25 +0000 Subject: [PATCH] [hs] Clean up 2020_18 --- hs/src/Aoc/Y2020/D18.hs | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/hs/src/Aoc/Y2020/D18.hs b/hs/src/Aoc/Y2020/D18.hs index b3e6a2b..45a1e19 100644 --- a/hs/src/Aoc/Y2020/D18.hs +++ b/hs/src/Aoc/Y2020/D18.hs @@ -14,44 +14,33 @@ import qualified Text.Megaparsec.Char.Lexer as L import Aoc.Day import Aoc.Parse -data Expr - = Lit Int - | Add Expr Expr - | Mul Expr Expr - deriving (Show) - -eval :: Expr -> Int -eval (Lit l) = l -eval (Add a b) = eval a + eval b -eval (Mul a b) = eval a * eval b - lexeme :: Parser a -> Parser a lexeme = L.lexeme (void $ lineWhile isSpace) symbol :: T.Text -> Parser T.Text symbol = L.symbol (void $ lineWhile isSpace) -parser :: [[Operator Parser Expr]] -> Parser [Expr] +parser :: [[Operator Parser Int]] -> Parser [Int] parser table = manyLines expr where parens = between (symbol "(") (symbol ")") - term = (Lit <$> lexeme L.decimal) <|> parens expr + term = lexeme L.decimal <|> parens expr expr = makeExprParser term table -table1 :: [[Operator Parser Expr]] -table1 = [[InfixL (Add <$ symbol "+"), InfixL (Mul <$ symbol "*")]] +table1 :: [[Operator Parser Int]] +table1 = [[InfixL ((+) <$ symbol "+"), InfixL ((*) <$ symbol "*")]] -table2 :: [[Operator Parser Expr]] -table2 = [[InfixL (Add <$ symbol "+")], [InfixL (Mul <$ symbol "*")]] +table2 :: [[Operator Parser Int]] +table2 = [[InfixL ((+) <$ symbol "+")], [InfixL ((*) <$ symbol "*")]] solver :: FilePath -> T.Text -> IO () solver path text = do putStrLn ">> Part 1" - parseAndSolve path text (parser table1) $ print . sum . map eval + parseAndSolve path text (parser table1) $ print . sum putStrLn "" putStrLn ">> Part 2" - parseAndSolve path text (parser table2) $ print . sum . map eval + parseAndSolve path text (parser table2) $ print . sum day :: Day day = dayText solver