2023 Day 16 Part 2 optimisation

We reduce the number of items we put on the list of beams to analyse.
Saving memory allocations list modifications.
This commit is contained in:
2023-12-16 09:46:46 +00:00
parent a3dc84770a
commit a40aaa9d37

View File

@@ -126,11 +126,7 @@ private:
while (!beams.empty()) {
auto beam{beams.front()};
beams.pop_front();
if (!loc_valid(beam.loc_)) { continue; }
// Insert into hash of energised locations. If that fails we've done this before and don't
// need to do it again.
if (auto [it, success] = energised_.insert(beam); !success) { continue; }
while (loc_valid(beam.loc_) && energised_.insert(beam).second) {
switch (grid_[beam.loc_.second][beam.loc_.first]) {
case '.':
break;
@@ -169,9 +165,8 @@ private:
default:
std::abort();
}
beam.loc_ = beam.loc_ + beam.dir_;
beams.push_back(beam);
}
}
}