aocpp/2022/21.cpp

190 lines
5.0 KiB
C++
Raw Normal View History

2023-04-04 20:58:59 -07:00
#include <cstdint>
#include <iostream>
#include <sstream>
2023-04-07 09:45:55 -07:00
#include <stdexcept>
2023-04-04 20:58:59 -07:00
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>
#include <boost/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <doctest.h>
2023-04-06 20:58:54 -07:00
#include <z3++.h>
2023-04-04 20:58:59 -07:00
#include <aocpp/Overloaded.hpp>
2023-04-08 12:08:51 -07:00
#include <aocpp/Parsing.hpp>
2023-04-04 20:58:59 -07:00
#include <aocpp/Startup.hpp>
namespace {
namespace phx = boost::phoenix;
namespace qi = boost::spirit::qi;
enum class Op { Add, Sub, Mul, Div };
struct Expr {
std::string lhs;
std::string rhs;
Op op;
};
struct Entry {
std::string lvalue;
std::variant<std::int64_t, Expr> rvalue;
};
using Input = std::vector<Entry>;
// Input file grammar
2023-04-08 12:08:51 -07:00
class Grammar : public qi::grammar<std::string::const_iterator, Input()> {
qi::rule<iterator_type, std::string()> variable;
qi::rule<iterator_type, Op()> op;
qi::rule<iterator_type, Expr()> expr;
qi::rule<iterator_type, std::variant<std::int64_t, Expr>()> rhs;
qi::rule<iterator_type, Entry()> line;
qi::rule<iterator_type, Input()> input;
2023-04-04 20:58:59 -07:00
public:
Grammar() : Grammar::base_type{input} {
using namespace qi::labels;
2023-04-07 09:07:32 -07:00
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";
2023-04-04 20:58:59 -07:00
input = *line;
}
};
auto Eval(
2023-04-04 21:11:22 -07:00
std::unordered_map<std::string, double> & values,
2023-04-04 20:58:59 -07:00
std::unordered_map<std::string, Expr> const& exprs,
std::string const& var)
2023-04-04 21:11:22 -07:00
-> double
2023-04-04 20:58:59 -07:00
{
2023-04-07 09:07:32 -07:00
if (auto const it = values.find(var); it != values.end()) {
2023-04-04 20:58:59 -07:00
return it->second;
}
auto const& [lhs,rhs,op] = exprs.at(var);
auto const l = Eval(values, exprs, lhs);
auto const r = Eval(values, exprs, rhs);
2023-04-04 21:11:22 -07:00
double o;
2023-04-04 20:58:59 -07:00
switch (op) {
case Op::Add: o = l + r; break;
case Op::Sub: o = l - r; break;
case Op::Mul: o = l * r; break;
case Op::Div: o = l / r; break;
}
values.emplace(var, o);
return o;
}
auto Part1(Input const& input) -> std::int64_t
{
2023-04-04 21:11:22 -07:00
std::unordered_map<std::string, double> values;
2023-04-04 20:58:59 -07:00
std::unordered_map<std::string, Expr> exprs;
for (auto const& entry : input) {
std::visit(overloaded {
[&](Expr const& expr) { exprs.emplace(entry.lvalue, expr); },
[&](std::int64_t val) { values.emplace(entry.lvalue, val); }
}, entry.rvalue);
}
return Eval(values, exprs, "root");
}
auto Part2(Input const& input) -> std::int64_t
{
2023-04-06 20:58:54 -07:00
auto ctx = z3::context{};
2023-04-07 09:07:32 -07:00
auto const solve_eqs = z3::tactic{ctx, "solve-eqs"};
auto const smt = z3::tactic{ctx, "smt"};
2023-04-06 20:58:54 -07:00
auto solver = (solve_eqs & smt).mk_solver();
std::unordered_map<std::string, z3::expr> constants;
2023-04-04 20:58:59 -07:00
for (auto const& entry : input) {
2023-04-06 20:58:54 -07:00
constants.emplace(entry.lvalue, ctx.int_const(entry.lvalue.c_str()));
2023-04-04 20:58:59 -07:00
}
2023-04-06 20:58:54 -07:00
for (auto const& entry : input) {
if (entry.lvalue == "root") {
if (auto * const expr = std::get_if<Expr>(&entry.rvalue)) {
solver.add(constants.at(expr->lhs) == constants.at(expr->rhs));
} else {
throw std::runtime_error{"malformed root"};
}
} else if (entry.lvalue != "humn") {
auto const rhs =
std::visit(overloaded {
2023-04-07 09:07:32 -07:00
[&](std::int64_t literal) {
2023-04-06 20:58:54 -07:00
return ctx.int_val(literal);
},
2023-04-07 09:07:32 -07:00
[&](Expr const& expr) {
2023-04-06 20:58:54 -07:00
auto const l = constants.at(expr.lhs);
auto const r = constants.at(expr.rhs);
switch (expr.op) {
case Op::Add: return l + r;
case Op::Sub: return l - r;
case Op::Mul: return l * r;
case Op::Div:
solver.add(l % r == 0);
return l / r;
}
}
}, entry.rvalue);
solver.add(constants.at(entry.lvalue) == rhs);
}
2023-04-04 20:58:59 -07:00
}
2023-04-06 21:07:44 -07:00
2023-04-06 20:58:54 -07:00
if (solver.check() != z3::sat) {
throw std::runtime_error{"no solution to part 2"};
}
2023-04-06 21:07:44 -07:00
return solver.get_model().eval(constants.at("humn")).get_numeral_int64();
2023-04-04 20:58:59 -07:00
}
} // namespace
TEST_SUITE("2022-21 examples") {
TEST_CASE("documented example") {
auto in = std::istringstream{
"root: pppw + sjmn\n"
"dbpl: 5\n"
"cczh: sllz + lgvd\n"
"zczc: 2\n"
"ptdq: humn - dvpt\n"
"dvpt: 3\n"
"lfqf: 4\n"
"humn: 5\n"
"ljgn: 2\n"
"sjmn: drzm * dbpl\n"
"sllz: 4\n"
"pppw: cczh / lfqf\n"
"lgvd: ljgn * ptdq\n"
"drzm: hmdt - zczc\n"
"hmdt: 32\n"
};
2023-04-08 12:08:51 -07:00
auto const input = aocpp::ParseGrammar(Grammar(), in);
2023-04-04 20:58:59 -07:00
CHECK(152 == Part1(input));
CHECK(301 == Part2(input));
}
}
auto Main(std::istream & in, std::ostream & out) -> void
2023-04-08 12:08:51 -07:00
{
auto const input = aocpp::ParseGrammar(Grammar(), in);
2023-04-04 20:58:59 -07:00
out << "Part 1: " << Part1(input) << std::endl;
out << "Part 2: " << Part2(input) << std::endl;
}