Dry run for 2022 day 19 part 2

This is slow, but will check everything.  I want to see whether being greedy will be better.
This commit is contained in:
2022-12-19 15:03:39 +00:00
parent 14f4274bdd
commit 564c75a5c6
2 changed files with 209 additions and 11 deletions

View File

@@ -11,7 +11,6 @@
#include <set>
#include <stdexcept>
#include <utility>
#include <vector>
using Int = std::int64_t;
using UInt = std::uint64_t;
@@ -53,22 +52,42 @@ struct State
std::array<UInt, resource_count> robots_available_;
};
struct StateCompare
{
auto operator()(State const& lhs, State const& rhs) const noexcept -> bool
{
for (UInt r{0}; r < resource_count; ++r) {
if (lhs.resources_available_[r] < rhs.resources_available_[r]) {
return true;
}
if (lhs.resources_available_[r] > rhs.resources_available_[r]) {
return false;
}
if (lhs.robots_available_[r] < rhs.robots_available_[r]) {
return true;
}
if (lhs.robots_available_[r] > rhs.robots_available_[r]) {
return false;
}
}
return false;
}
};
using StateSet = std::set<State, StateCompare>;
auto generate(Costs const& costs) -> UInt
{
UInt max_geode_count{0};
constexpr UInt total_time{24};
std::list<State> next_states{State()};
StateSet next_states;
next_states.insert(State{});
for (UInt t{0}; t < total_time; ++t) {
std::cout << " Time " << t << ": " << next_states.size()
<< " States to consider. Max geode: " << max_geode_count << "\n";
std::list<State> const current_states{std::move(next_states)};
next_states = std::list<State>();
StateSet const current_states{std::move(next_states)};
next_states = StateSet{};
for (auto const& state : current_states) {
// See if this is the best state we've seen:
max_geode_count = std::max(max_geode_count, state.resources_available_[GEODE]);
// Let's build a robot:
for (UInt robot{0}; robot < resource_count; ++robot) {
bool build{true};
@@ -84,7 +103,11 @@ auto generate(Costs const& costs) -> UInt
new_state.resources_available_[resource] += state.robots_available_[resource];
}
new_state.robots_available_[robot] += 1;
next_states.push_back(new_state);
auto [it, success] = next_states.insert(new_state);
if (success) {
// See if this is the best state we've seen:
max_geode_count = std::max(max_geode_count, new_state.resources_available_[GEODE]);
}
}
}
@@ -93,9 +116,15 @@ auto generate(Costs const& costs) -> UInt
for (UInt resource{0}; resource < resource_count; ++resource) {
new_state.resources_available_[resource] += state.robots_available_[resource];
}
next_states.push_back(new_state);
auto [it, success] = next_states.insert(new_state);
if (success) {
// See if this is the best state we've seen:
max_geode_count = std::max(max_geode_count, new_state.resources_available_[GEODE]);
}
}
}
return max_geode_count;
}
auto main() -> int