diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2022/day12/README.md | 25 | ||||
-rw-r--r-- | src/2022/day12/aoc.cpp | 10 | ||||
-rw-r--r-- | src/2022/day12/aoc.h | 75 |
3 files changed, 53 insertions, 57 deletions
diff --git a/src/2022/day12/README.md b/src/2022/day12/README.md index d6123d2..0a0d0f4 100644 --- a/src/2022/day12/README.md +++ b/src/2022/day12/README.md @@ -30,4 +30,29 @@ This path reaches the goal in 31 steps, the fewest possible. What is the fewest steps required to move from your current position to the location that should get the best signal? +--- Part Two --- + +As you walk up the hill, you suspect that the Elves will want to turn this into a hiking trail. The beginning isn't very scenic, though; perhaps you can find a better starting point. + +To maximize exercise while hiking, the trail should start as low as possible: elevation a. The goal is still the square marked E. However, the trail should still be direct, taking the fewest steps to reach its goal. So, you'll need to find the shortest path from any square at elevation a to the square marked E. + +Again consider the example from above: + +Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi + +Now, there are six choices for starting position (five marked a, plus the square marked S that counts as being at elevation a). If you start at the bottom-left square, you can reach the goal most quickly: + +...v<<<< +...vv<<^ +...v>E^^ +.>v>>>^^ +>^>>>>>^ + +This path reaches the goal in only 29 steps, the fewest possible. + +What is the fewest steps required to move starting from any square with elevation a to the location that should get the best signal? diff --git a/src/2022/day12/aoc.cpp b/src/2022/day12/aoc.cpp index 4cc388d..34913e2 100644 --- a/src/2022/day12/aoc.cpp +++ b/src/2022/day12/aoc.cpp @@ -10,15 +10,9 @@ std::pair<int, int> day12(line_view file) { hm.load(row++, lv); return true; }); + hm.flood(hm.start); - // hm.print(); - std::vector<int> steps; - hm.find(hm.end, steps); - - for(auto& i : steps) { - printf("%d\n", i); - } - return {0, 0}; + return {hm.was(hm.end), 0}; } } diff --git a/src/2022/day12/aoc.h b/src/2022/day12/aoc.h index a7380b2..7d786b4 100644 --- a/src/2022/day12/aoc.h +++ b/src/2022/day12/aoc.h @@ -19,13 +19,22 @@ struct heightmap { struct height { char h; - int v = 0; + int v = INT32_MAX; }; height heights[row * col]; pos start; pos end; + char* indent (int s) { + static char space[100] = {0}; + memset(space, 0 ,100); + for(int i = 0; i < s; i++) { + space[i] = ' '; + } + return space; + }; + char& get(pos p) { return heights[p.y * col + p.x].h; } @@ -46,7 +55,7 @@ struct heightmap { {p.x + 1, p.y}, // RIGHT }; for (int i = 0; i < 4; i++) { - if (valid(ps[i]) && !was(ps[i])) { + if (valid(ps[i])) { bool b1 = get(ps[i]) == get(p) + 1; bool b2 = get(ps[i]) <= get(p); if (b1 || b2) { @@ -57,60 +66,29 @@ struct heightmap { } } - void get_prev(pos p, pos prev[], int* n) { - pos ps[] = { - {p.x, p.y - 1}, // UP - {p.x, p.y + 1}, // DOWN - {p.x - 1, p.y}, // LEFT - {p.x + 1, p.y}, // RIGHT - }; - for (int i = 0; i < 4; i++) { - if (valid(ps[i])) { - bool b1 = get(ps[i]) == get(p) - 1; - bool b2 = get(ps[i]) == get(p); - if (b1 || b2) { - prev[*n] = ps[i]; - *n += 1; + void flood(pos p) { + was(p) = 0; + std::deque<pos> q = {p}; + while (!q.empty()) { + pos px = q.front(); + q.pop_front(); + pos next[4]; + int n{0}; + get_next(px, next, &n); + for (int i = 0; i < n; i++){ + int x = was(px) + 1; + if (x < was(next[i])) { + was(next[i]) = x; + q.push_back(next[i]); } } } } - char * indent (int s) { - static char space[100] = {0}; - memset(space, 0 ,100); - for(int i = 0; i < s; i++) { - space[i] = ' '; - } - return space; - }; - - void find(pos p, std::vector<int>& paths) { - std::deque<pos> q = {p}; - int steps{0}; - while (!q.empty()) { - steps += 1; - pos px = q.front(); - q.pop_front(); - was(px) += 1; - pos ps[4]; - int n{0}; - get_prev(px, ps, &n); - for (int i = 0; i < n; i++) { - if (ps[i] == start) { - paths.push_back(steps); - } - else { - q.push_back(ps[i]); - } - } - } - } - void print() { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { - printf("%c", get({j, i})); + printf("%c ", get({j, i})); } printf("\n"); } @@ -128,7 +106,6 @@ struct heightmap { end = pos{c, r}; get({c, r}) = 'z'; } - // printf("(%d, %d) %c\n", c, r, get({c, r})); } } }; |