aocpp/2022/11.cpp

163 lines
4.2 KiB
C++
Raw Normal View History

2023-01-25 07:04:46 -08:00
#include <cstdint>
#include <iostream>
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
2023-05-03 14:08:50 -07:00
#include <boost/phoenix.hpp>
#include <boost/range/adaptor/indexed.hpp>
#include <boost/spirit/include/qi.hpp>
2023-01-25 13:48:54 -08:00
2023-01-25 07:04:46 -08:00
#include <doctest.h>
2023-05-03 14:08:50 -07:00
#include <aocpp/Parsing.hpp>
2023-01-25 07:04:46 -08:00
#include <aocpp/Startup.hpp>
namespace {
2023-05-03 14:08:50 -07:00
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
2023-01-25 07:04:46 -08:00
struct Monkey {
std::vector<std::int64_t> items;
std::int64_t divisor;
std::size_t true_ix, false_ix;
std::optional<std::int64_t> rhs;
char op;
2023-05-03 14:08:50 -07:00
auto throw_to(std::int64_t const item) const -> std::size_t {
return item % divisor == 0 ? true_ix : false_ix;
}
2023-01-25 07:04:46 -08:00
2023-05-03 14:08:50 -07:00
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"};
2023-01-25 07:04:46 -08:00
}
2023-05-03 14:08:50 -07:00
return item;
2023-01-25 07:04:46 -08:00
}
2023-05-03 14:08:50 -07:00
};
2023-01-25 07:04:46 -08:00
2023-05-03 14:08:50 -07:00
struct Grammar : public qi::grammar<std::string::const_iterator, std::vector<Monkey>()> {
qi::rule<iterator_type, Monkey()> monkey;
qi::rule<iterator_type, std::vector<Monkey>()> monkies;
Grammar() : base_type{monkies} {
using namespace qi::labels;
monkies = monkey % "\n";
monkey =
"Monkey " >>
qi::omit[qi::ulong_long] >>
":\n Starting items: " >>
(qi::long_long % ", ") [ phx::bind(&Monkey::items, _val) = _1 ] >>
"\n Operation: new = old " >>
qi::char_ [ phx::bind(&Monkey::op, _val) = _1 ] >>
" " >>
("old" | qi::long_long [ phx::bind(&Monkey::rhs, _val) = _1 ]) >>
"\n Test: divisible by " >>
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 ] >>
"\n If false: throw to monkey " >>
qi::ulong_long [ phx::bind(&Monkey::false_ix, _val) = _1 ] >>
"\n";
}
};
2023-01-25 07:04:46 -08:00
auto Solve(std::vector<Monkey> const& input, std::size_t const rounds, std::optional<std::int64_t> const modulus) -> std::int64_t
{
2023-05-03 14:08:50 -07:00
auto throws = std::vector<std::int64_t>(input.size());
for (auto const& [start, monkey] : boost::adaptors::index(input)) {
for (auto item : monkey.items) {
auto cursor = static_cast<std::size_t>(start);
2023-01-25 07:04:46 -08:00
for (auto n = rounds; n > 0;) {
throws[cursor]++;
auto const& m = input[cursor];
2023-05-03 14:08:50 -07:00
item = m.operation(item);
if (modulus) {
2023-01-25 07:04:46 -08:00
item %= *modulus;
} else {
item /= 3;
}
2023-05-03 14:08:50 -07:00
auto const next = m.throw_to(item);
2023-01-25 07:04:46 -08:00
if (next < cursor) n--;
cursor = next;
}
}
}
// Compute top 2 elements of the vector
std::partial_sort(throws.begin(), throws.begin()+2, throws.end(), std::greater());
return throws[0] * throws[1];
}
auto Part1(std::vector<Monkey> const& input) -> std::int64_t
{
return Solve(input, 20, {});
}
auto Part2(std::vector<Monkey> const& input) -> std::int64_t
{
auto const modulus =
std::transform_reduce(
2023-05-03 14:08:50 -07:00
input.begin(), input.end(),
2023-01-25 07:04:46 -08:00
std::int64_t{1}, std::lcm<std::int64_t, std::int64_t>,
2023-05-03 14:08:50 -07:00
[](auto const& m) { return m.divisor; });
2023-01-25 07:04:46 -08:00
return Solve(input, 10'000, modulus);
}
} // namespace
TEST_SUITE("2022-11 examples") {
TEST_CASE("example") {
std::istringstream in {
R"(Monkey 0:
Starting items: 79, 98
Operation: new = old * 19
Test: divisible by 23
If true: throw to monkey 2
If false: throw to monkey 3
Monkey 1:
Starting items: 54, 65, 75, 74
Operation: new = old + 6
Test: divisible by 19
If true: throw to monkey 2
If false: throw to monkey 0
Monkey 2:
Starting items: 79, 60, 97
Operation: new = old * old
Test: divisible by 13
If true: throw to monkey 1
If false: throw to monkey 3
Monkey 3:
Starting items: 74
Operation: new = old + 3
Test: divisible by 17
If true: throw to monkey 0
If false: throw to monkey 1
)"
};
2023-05-03 14:08:50 -07:00
auto const input = aocpp::ParseGrammar(Grammar{}, in);
2023-05-03 10:56:52 -07:00
CHECK(Part1(input) == 10'605);
CHECK(Part2(input) == 2'713'310'158);
2023-01-25 07:04:46 -08:00
}
}
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-25 07:04:46 -08:00
{
2023-05-03 14:08:50 -07:00
auto const input = aocpp::ParseGrammar(Grammar{}, in);
2023-01-31 09:15:15 -08:00
out << "Part 1: " << Part1(input) << std::endl;
out << "Part 2: " << Part2(input) << std::endl;
2023-01-25 07:04:46 -08:00
}