Skip to content

Commit

Permalink
2023 day 23: convert construct_trails() to be iterative
Browse files Browse the repository at this point in the history
  • Loading branch information
yut23 committed Jan 27, 2024
1 parent 614770a commit 0029472
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions 2023/src/day23.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TrailMap {
get_grid_neighbors(const Pos &pos, const Pos &prev_pos) const;

void add_edge(const Pos &from, const Pos &to, int distance);
void construct_trails(const Pos start, const Pos next);
void construct_trails(Pos start);

std::pair<Pos, Pos> dist_key(const Pos &from, const Pos &to) const {
return std::minmax(from, to);
Expand Down Expand Up @@ -70,7 +70,7 @@ TrailMap::TrailMap(const std::vector<std::string> &grid_) : grid(grid_) {
// starting point
Pos start{1, 0};
assert(grid[start] == '.');
construct_trails(start, start);
construct_trails(start);
}

std::pair<std::vector<Pos>, bool>
Expand Down Expand Up @@ -123,28 +123,33 @@ void TrailMap::add_edge(const Pos &from, const Pos &to, int distance) {
distances[dist_key(from, to)] = distance;
}

void TrailMap::construct_trails(const Pos start, const Pos next) {
Pos prev_pos = start;
Pos curr_pos = next;
int length = start == next ? 0 : 1;
while (true) {
const auto [neighbors, is_junction] =
get_grid_neighbors(curr_pos, prev_pos);
if (!is_junction && neighbors.size() == 1) {
++length;
prev_pos = curr_pos;
curr_pos = neighbors.front();
} else {
// recurse on each of the outgoing paths (if any)
for (const Pos &neighbor : neighbors) {
construct_trails(curr_pos, neighbor);
void TrailMap::construct_trails(Pos start) {
std::queue<std::pair<Pos, Pos>> pending;
pending.emplace(start, start);
while (!pending.empty()) {
auto [prev_pos, curr_pos] = pending.front();
start = prev_pos;
pending.pop();
int length = prev_pos == curr_pos ? 0 : 1;
while (true) {
const auto [neighbors, is_junction] =
get_grid_neighbors(curr_pos, prev_pos);
if (!is_junction && neighbors.size() == 1) {
++length;
prev_pos = curr_pos;
curr_pos = neighbors.front();
} else {
// recurse on each of the outgoing paths (if any)
for (const Pos &neighbor : neighbors) {
pending.emplace(curr_pos, neighbor);
}
break;
}
break;
}
}
if (length > 0) {
// add a path from start to pos
add_edge(start, curr_pos, length);
if (length > 0) {
// add a path from start to pos
add_edge(start, curr_pos, length);
}
}
}

Expand Down

0 comments on commit 0029472

Please sign in to comment.