boost for parsing 2022-11

This commit is contained in:
Eric Mertens 2023-05-03 14:08:50 -07:00
parent f4e7713231
commit fce18c81b3

View File

@ -6,79 +6,87 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <boost/range/irange.hpp> #include <boost/phoenix.hpp>
#include <boost/range/adaptor/indexed.hpp>
#include <boost/spirit/include/qi.hpp>
#include <doctest.h> #include <doctest.h>
#include <aocpp/Parsing.hpp>
#include <aocpp/Startup.hpp> #include <aocpp/Startup.hpp>
namespace { namespace {
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
struct Monkey { struct Monkey {
std::vector<std::int64_t> items; std::vector<std::int64_t> items;
std::int64_t divisor; std::int64_t divisor;
std::size_t true_ix, false_ix; std::size_t true_ix, false_ix;
std::optional<std::int64_t> rhs; std::optional<std::int64_t> rhs;
char op; char op;
auto throw_to(std::int64_t const item) const -> std::size_t {
return item % divisor == 0 ? true_ix : false_ix;
}
auto operation(std::int64_t const item) const -> std::int64_t {
auto const y = rhs.value_or(item);
switch (op) {
case '+': return item + y;
case '*': return item * y;
default: throw std::runtime_error{"bad input"};
}
return item;
}
}; };
auto Parse(std::istream & in) -> std::vector<Monkey> struct Grammar : public qi::grammar<std::string::const_iterator, std::vector<Monkey>()> {
{ qi::rule<iterator_type, Monkey()> monkey;
std::string word; qi::rule<iterator_type, std::vector<Monkey>()> monkies;
std::vector<Monkey> monkies;
while (in >> word) { Grammar() : base_type{monkies} {
monkies.emplace_back(); using namespace qi::labels;
Monkey & m = monkies.back(); monkies = monkey % "\n";
monkey =
in >> word >> word >> word; // 0: Starting items: "Monkey " >>
qi::omit[qi::ulong_long] >>
for (;;) { ":\n Starting items: " >>
std::int64_t elt; (qi::long_long % ", ") [ phx::bind(&Monkey::items, _val) = _1 ] >>
in >> elt; "\n Operation: new = old " >>
m.items.push_back(elt); qi::char_ [ phx::bind(&Monkey::op, _val) = _1 ] >>
" " >>
char c; ("old" | qi::long_long [ phx::bind(&Monkey::rhs, _val) = _1 ]) >>
in >> c; "\n Test: divisible by " >>
if (c != ',') break; qi::long_long [ phx::bind(&Monkey::divisor, _val) = _1 ] >>
} "\n If true: throw to monkey " >>
qi::ulong_long [ phx::bind(&Monkey::true_ix, _val) = _1 ] >>
in >> word >> word >> word >> word; // Operation: new = old "\n If false: throw to monkey " >>
qi::ulong_long [ phx::bind(&Monkey::false_ix, _val) = _1 ] >>
in >> m.op >> word; "\n";
if (word != "old") m.rhs = std::stoll(word);
in >> word >> word >> word >> m.divisor;
in >> word >> word >> word >> word >> word >> m.true_ix
>> word >> word >> word >> word >> word >> m.false_ix;
}
return monkies;
} }
};
auto Solve(std::vector<Monkey> const& input, std::size_t const rounds, std::optional<std::int64_t> const modulus) -> std::int64_t auto Solve(std::vector<Monkey> const& input, std::size_t const rounds, std::optional<std::int64_t> const modulus) -> std::int64_t
{ {
std::vector<std::int64_t> throws(input.size()); auto throws = std::vector<std::int64_t>(input.size());
for (auto const i : boost::irange(input.size())) { for (auto const& [start, monkey] : boost::adaptors::index(input)) {
for (auto item : input[i].items) { for (auto item : monkey.items) {
auto cursor = i; auto cursor = static_cast<std::size_t>(start);
for (auto n = rounds; n > 0;) { for (auto n = rounds; n > 0;) {
throws[cursor]++; throws[cursor]++;
auto const& m = input[cursor]; auto const& m = input[cursor];
auto const y = m.rhs.value_or(item); item = m.operation(item);
switch (m.op) {
case '+': item += y; break; if (modulus) {
case '*': item *= y; break;
default: throw std::runtime_error{"bad input"};
}
if (modulus.has_value()) {
item %= *modulus; item %= *modulus;
} else { } else {
item /= 3; item /= 3;
} }
auto const next = (item % m.divisor == 0) ? m.true_ix : m.false_ix; auto const next = m.throw_to(item);
if (next < cursor) n--; if (next < cursor) n--;
cursor = next; cursor = next;
} }
@ -100,9 +108,9 @@ auto Part2(std::vector<Monkey> const& input) -> std::int64_t
{ {
auto const modulus = auto const modulus =
std::transform_reduce( std::transform_reduce(
input.cbegin(), input.cend(), input.begin(), input.end(),
std::int64_t{1}, std::lcm<std::int64_t, std::int64_t>, std::int64_t{1}, std::lcm<std::int64_t, std::int64_t>,
[](Monkey const& m) { return m.divisor; }); [](auto const& m) { return m.divisor; });
return Solve(input, 10'000, modulus); return Solve(input, 10'000, modulus);
} }
@ -140,7 +148,7 @@ Monkey 3:
If false: throw to monkey 1 If false: throw to monkey 1
)" )"
}; };
auto const input = Parse(in); auto const input = aocpp::ParseGrammar(Grammar{}, in);
CHECK(Part1(input) == 10'605); CHECK(Part1(input) == 10'605);
CHECK(Part2(input) == 2'713'310'158); CHECK(Part2(input) == 2'713'310'158);
} }
@ -148,7 +156,7 @@ Monkey 3:
auto Main(std::istream & in, std::ostream & out) -> void auto Main(std::istream & in, std::ostream & out) -> void
{ {
auto const input {Parse(in)}; auto const input = aocpp::ParseGrammar(Grammar{}, in);
out << "Part 1: " << Part1(input) << std::endl; out << "Part 1: " << Part1(input) << std::endl;
out << "Part 2: " << Part2(input) << std::endl; out << "Part 2: " << Part2(input) << std::endl;
} }