Update 20.cpp

This commit is contained in:
Eric Mertens 2022-11-10 21:25:27 -08:00
parent 17728edb44
commit 3d665e5ebb

View File

@ -118,11 +118,11 @@ auto FindDistances(Map const& map, Portals const& portals) {
Distances distances; Distances distances;
std::map<Coord, Name> names; std::map<Coord, Name> names;
for (auto [k,v] : portals) { for (auto const& [k,v] : portals) {
names[v] = k; names[v] = k;
} }
for (auto const [start_name, start_coord] : portals) { for (auto const& [start_name, start_coord] : portals) {
distances[start_name] = FindDistancesFrom(map, names, distances, start_name, start_coord); distances[start_name] = FindDistancesFrom(map, names, distances, start_name, start_coord);
} }
@ -132,11 +132,11 @@ auto FindDistances(Map const& map, Portals const& portals) {
auto SolveMaze(Distances const& distances, bool const recursive) -> std::int64_t auto SolveMaze(Distances const& distances, bool const recursive) -> std::int64_t
{ {
// Track current positions and current set of keys in easy to compare form // Track current positions and current set of keys in easy to compare form
using Visited = std::pair<int, Name>; using Visited = std::pair<std::int64_t, Name>;
std::set<Visited> seen; std::set<Visited> seen;
// Priority queue returning lowest path cost states first. // Priority queue returning lowest path cost states first.
using PqElt = std::tuple<std::int64_t, int, Name>; using PqElt = std::tuple<std::int64_t, std::int64_t, Name>;
using PqCmp = decltype([](PqElt const& x, PqElt const& y) { using PqCmp = decltype([](PqElt const& x, PqElt const& y) {
return std::get<0>(x) > std::get<0>(y); }); return std::get<0>(x) > std::get<0>(y); });
std::priority_queue<PqElt, std::vector<PqElt>, PqCmp> todo; std::priority_queue<PqElt, std::vector<PqElt>, PqCmp> todo;
@ -144,7 +144,7 @@ auto SolveMaze(Distances const& distances, bool const recursive) -> std::int64_t
todo.emplace(0, 0, "-AA"); todo.emplace(0, 0, "-AA");
while(!todo.empty()) { while(!todo.empty()) {
auto [steps, depth, name] = todo.top(); auto const [steps, depth, name] = todo.top();
todo.pop(); todo.pop();
if (name == "-ZZ") { return steps; } if (name == "-ZZ") { return steps; }
if (seen.emplace(depth, name).second) { if (seen.emplace(depth, name).second) {