Well today was done in less that 10 minutes, which I have to say was nice as I had a lot on today :-) Parsed expressions into AST, as I have multiple Haskell parsers for DSP programming languages that I've developed over the last few years and so it was a simple cut paste, rename, and factorize for part one and two to work with the same parser.
dataExpr=NumInteger|OpOpExprExprderivingShowdataOp=Add|MulderivingShoweval::([Parser(Expr->Expr->Expr)],[Parser(Expr->Expr->Expr)])->String->Integerevalops=eval'.parseExpropseval'::Expr->Integereval'(Numx)=xeval'(OpAddxy)=eval'x+eval'yeval'(OpMulxy)=eval'x*eval'y-- ParserparseExpr::([Parser(Expr->Expr->Expr)],[Parser(Expr->Expr->Expr)])->String->ExprparseExpropss=caseparse(spaces*>expressionops<*eof)""sofLefte->error$showeRightx->xexpression::([Parser(Expr->Expr->Expr)],[Parser(Expr->Expr->Expr)])->ParserExprexpressionops=lowerExprops<|>higherExprops<|>number<|>parens(expressionops)lowerExpr::([Parser(Expr->Expr->Expr)],[Parser(Expr->Expr->Expr)])->ParserExprlowerExpr(ops,ops')=try$expression'`chainl1`operatorwhereexpression'=higherExpr(ops,ops')<|>number<|>parens(expression(ops,ops'))operator=choiceops<*spaceshigherExpr::([Parser(Expr->Expr->Expr)],[Parser(Expr->Expr->Expr)])->ParserExprhigherExpr(ops,ops')=try$expression'`chainl1`operatorwhereexpression'=number<|>parens(expression(ops,ops'))operator=choiceops'<*spacesnumber::ParserExprnumber=dodigits<-many1digitspacesreturn$Num$readdigitsparens::Parsera->Parseraparens=betweenopenclosewhereopen=char'('<*spacesclose=char')'<*spaces-- lower and higer precedence ops for task1task1::([Parser(Expr->Expr->Expr)],[Parser(Expr->Expr->Expr)])task1=([OpAdd<$char'+',OpMul<$char'*'],[])-- lower and higer precedence ops for task2task2::([Parser(Expr->Expr->Expr)],[Parser(Expr->Expr->Expr)])task2=([OpMul<$char'*'],[OpAdd<$char'+'])main=dois<-readFile"day18_input"<&>linesprint(sum$map(evaltask1)is)print(sum$map(evaltask2)is)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Well today was done in less that 10 minutes, which I have to say was nice as I had a lot on today :-) Parsed expressions into AST, as I have multiple Haskell parsers for DSP programming languages that I've developed over the last few years and so it was a simple cut paste, rename, and factorize for part one and two to work with the same parser.