From 930a02622df31929bd1ef554b4e0d2fd677b3d1a Mon Sep 17 00:00:00 2001 From: Eric Mertens Date: Fri, 7 Apr 2023 09:07:32 -0700 Subject: [PATCH] fiddle --- 2022/21.cpp | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/2022/21.cpp b/2022/21.cpp index f2b4347..1c700e2 100644 --- a/2022/21.cpp +++ b/2022/21.cpp @@ -50,21 +50,20 @@ public: Grammar() : Grammar::base_type{input} { using namespace qi::labels; - variable %= qi::as_string[+qi::alpha]; - op = qi::string("+") [ _val = Op::Add ] - | qi::string("-") [ _val = Op::Sub ] - | qi::string("*") [ _val = Op::Mul ] - | qi::string("/") [ _val = Op::Div ]; - expr = variable [phx::bind(&Expr::lhs, _val) = _1] - >> " " - >> op [phx::bind(&Expr::op, _val) = _1] - >> " " - >> variable [phx::bind(&Expr::rhs, _val) = _1]; - rhs = expr [ _val = _1 ] | qi::long_long [ _val = _1 ]; - line = variable [ phx::bind(&Entry::lvalue, _val) = _1] - >> ": " - >> rhs [ phx::bind(&Entry::rvalue, _val) = _1] - >> "\n"; + variable = qi::as_string[+qi::alpha]; + op = + qi::string("+") [ _val = Op::Add ] | + qi::string("-") [ _val = Op::Sub ] | + qi::string("*") [ _val = Op::Mul ] | + qi::string("/") [ _val = Op::Div ]; + expr = + variable [phx::bind(&Expr::lhs, _val) = _1] >> " " >> + op [phx::bind(&Expr::op, _val) = _1] >> " " >> + variable [phx::bind(&Expr::rhs, _val) = _1]; + rhs = expr | qi::long_long; + line = + variable [ phx::bind(&Entry::lvalue, _val) = _1] >> ": " >> + rhs [ phx::bind(&Entry::rvalue, _val) = _1] >> "\n"; input = *line; } }; @@ -91,8 +90,7 @@ auto Eval( std::string const& var) -> double { - auto it = values.find(var); - if (it != values.end()) { + if (auto const it = values.find(var); it != values.end()) { return it->second; } auto const& [lhs,rhs,op] = exprs.at(var); @@ -125,8 +123,8 @@ auto Part1(Input const& input) -> std::int64_t auto Part2(Input const& input) -> std::int64_t { auto ctx = z3::context{}; - auto solve_eqs = z3::tactic{ctx, "solve-eqs"}; - auto smt = z3::tactic{ctx, "smt"}; + auto const solve_eqs = z3::tactic{ctx, "solve-eqs"}; + auto const smt = z3::tactic{ctx, "smt"}; auto solver = (solve_eqs & smt).mk_solver(); std::unordered_map constants; @@ -144,10 +142,10 @@ auto Part2(Input const& input) -> std::int64_t } else if (entry.lvalue != "humn") { auto const rhs = std::visit(overloaded { - [&ctx](std::int64_t literal) { + [&](std::int64_t literal) { return ctx.int_val(literal); }, - [&ctx, &constants, &solver](Expr const& expr) { + [&](Expr const& expr) { auto const l = constants.at(expr.lhs); auto const r = constants.at(expr.rhs); switch (expr.op) {