2023 Day 19 add debug flag

This commit is contained in:
2023-12-19 11:47:47 +00:00
parent db8425d331
commit 16f240c930

View File

@@ -12,6 +12,8 @@
using Int = std::int64_t; using Int = std::int64_t;
using UInt = std::uint64_t; using UInt = std::uint64_t;
constexpr bool debug{true};
enum Op { greater, lessthan }; enum Op { greater, lessthan };
using Ratings = std::unordered_map<char, UInt>; using Ratings = std::unordered_map<char, UInt>;
@@ -45,7 +47,9 @@ struct Action
auto itnew{rnew.find(c_)}; auto itnew{rnew.find(c_)};
itnew->second.first = std::max(itnew->second.first, limit_ + 1); itnew->second.first = std::max(itnew->second.first, limit_ + 1);
if (itnew->second.first < itnew->second.second) { if (itnew->second.first < itnew->second.second) {
if (debug) {
std::cout << " " << next_ << ", " << rnew << '\n'; std::cout << " " << next_ << ", " << rnew << '\n';
}
work_list.emplace_back(next_, rnew); work_list.emplace_back(next_, rnew);
} }
@@ -58,7 +62,9 @@ struct Action
auto itnew{rnew.find(c_)}; auto itnew{rnew.find(c_)};
itnew->second.second = std::min(itnew->second.second, limit_); itnew->second.second = std::min(itnew->second.second, limit_);
if (itnew->second.first < itnew->second.second) { if (itnew->second.first < itnew->second.second) {
if (debug) {
std::cout << " " << next_ << ", " << rnew << '\n'; std::cout << " " << next_ << ", " << rnew << '\n';
}
work_list.emplace_back(next_, rnew); work_list.emplace_back(next_, rnew);
} }
@@ -85,7 +91,9 @@ struct Workflow
action.split(work_list, r); action.split(work_list, r);
} }
if (debug) {
std::cout << " " << default_ << ", " << r << '\n'; std::cout << " " << default_ << ", " << r << '\n';
}
work_list.emplace_back(default_, r); work_list.emplace_back(default_, r);
} }
@@ -155,19 +163,25 @@ struct Workflows
auto [rule, range] = work_list.front(); auto [rule, range] = work_list.front();
work_list.pop_front(); work_list.pop_front();
std::cout << " " << rule << ": " << range << '\n'; if (debug) {
std::cout << " " << rule << ": " << range;
}
bool work_to_do{true}; bool work_to_do{true};
for (auto const& r : range) { for (auto const& r : range) {
work_to_do &= (r.second.first < r.second.second); work_to_do &= (r.second.first < r.second.second);
} }
if (!work_to_do) { if (!work_to_do) {
std::cout << " No work to do.\n"; if (debug) {
std::cout << " = 0; # No work to do.\n";
}
continue; continue;
} }
if (rule == "R") { if (rule == "R") {
std::cout << " Reject.\n"; if (debug) {
std::cout << " = 0; # Reject.\n";
}
continue; continue;
} }
if (rule == "A") { if (rule == "A") {
@@ -176,16 +190,21 @@ struct Workflows
total *= r.second.second - r.second.first; total *= r.second.second - r.second.first;
} }
grand_total += total; grand_total += total;
std::cout << " Accept " << range << " = " << total << '\n'; if (debug) {
std::cout << " = " << total << '\n';
}
continue; continue;
} }
auto it = workflows_.find(rule); auto it = workflows_.find(rule);
assert(it != workflows_.end()); assert(it != workflows_.end());
if (debug) { std::cout << '\n'; }
it->second.append_ranges(work_list, range); it->second.append_ranges(work_list, range);
} }
if (debug) {
std::cout << " Grand total: " << grand_total << '\n'; std::cout << " Grand total: " << grand_total << '\n';
}
return grand_total; return grand_total;
} }
@@ -220,7 +239,7 @@ auto main() -> int try {
UInt total{0}; UInt total{0};
while (std::getline(std::cin, line)) { while (std::getline(std::cin, line)) {
Ratings const r = read_ratings(line); Ratings const r = read_ratings(line);
std::cout << line << ":\n"; if (debug) { std::cout << line << ":\n"; }
if (workflows.accept(r)) { if (workflows.accept(r)) {
for (auto [c, value] : r) { for (auto [c, value] : r) {
total += value; total += value;