diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-12-12 23:38:22 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-12-12 23:38:22 +0800 |
commit | 8579b920f5ae6bd14b795b92e8864e650e82b837 (patch) | |
tree | df463f76df4281db706357bf7dcd3749c5dfff71 /src | |
parent | 26c6c7f4d0df674b74434e05ea4965ba7ec8080a (diff) | |
download | advent-of-code-8579b920f5ae6bd14b795b92e8864e650e82b837.tar.gz advent-of-code-8579b920f5ae6bd14b795b92e8864e650e82b837.zip |
2022 day12 part1
Diffstat (limited to 'src')
-rw-r--r-- | src/2022/day12/README.md | 33 | ||||
-rw-r--r-- | src/2022/day12/aoc.cpp | 16 | ||||
-rw-r--r-- | src/2022/day12/aoc.h | 105 | ||||
-rw-r--r-- | src/2022/day12/input | 41 | ||||
-rw-r--r-- | src/2022/day12/input0 | 5 |
5 files changed, 199 insertions, 1 deletions
diff --git a/src/2022/day12/README.md b/src/2022/day12/README.md index e69de29..d6123d2 100644 --- a/src/2022/day12/README.md +++ b/src/2022/day12/README.md @@ -0,0 +1,33 @@ +--- Day 12: Hill Climbing Algorithm --- + +You try contacting the Elves using your handheld device, but the river you're following must be too low to get a decent signal. + +You ask the device for a heightmap of the surrounding area (your puzzle input). The heightmap shows the local area from above broken into a grid; the elevation of each square of the grid is given by a single lowercase letter, where a is the lowest elevation, b is the next-lowest, and so on up to the highest elevation, z. + +Also included on the heightmap are marks for your current position (S) and the location that should get the best signal (E). Your current position (S) has elevation a, and the location that should get the best signal (E) has elevation z. + +You'd like to reach E, but to save energy, you should do it in as few steps as possible. During each step, you can move exactly one square up, down, left, or right. To avoid needing to get out your climbing gear, the elevation of the destination square can be at most one higher than the elevation of your current square; that is, if your current elevation is m, you could step to elevation n, but not to elevation o. (This also means that the elevation of the destination square can be much lower than the elevation of your current square.) + +For example: + +Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi + +Here, you start in the top-left corner; your goal is near the middle. You could start by moving down or right, but eventually you'll need to head toward the e at the bottom. From there, you can spiral around to the goal: + +v..v<<<< +>v.vv<<^ +.>vv>E^^ +..v>>>^^ +..>>>>>^ + +In the above diagram, the symbols indicate whether the path exits each square moving up (^), down (v), left (<), or right (>). The location that should get the best signal is still E, and . marks unvisited squares. + +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? + + diff --git a/src/2022/day12/aoc.cpp b/src/2022/day12/aoc.cpp index 76a2384..c0d1db5 100644 --- a/src/2022/day12/aoc.cpp +++ b/src/2022/day12/aoc.cpp @@ -1,9 +1,23 @@ #include "aoc.h" +#include <algorithm> namespace aoc2022 { std::pair<int, int> day12(line_view file) { - return {0, 0}; + int row{0}; + heightmap hm; + per_line(file, [&hm, &row](line_view lv){ + hm.load(row++, lv); + return true; + }); + + hm.print(); + std::vector<int> paths; + hm.was(hm.start) += 1; + hm.find(0, hm.start, paths); + + std::sort(paths.begin(), paths.end()); + return {paths[0], 0}; } } diff --git a/src/2022/day12/aoc.h b/src/2022/day12/aoc.h index 6b959c6..a771bfc 100644 --- a/src/2022/day12/aoc.h +++ b/src/2022/day12/aoc.h @@ -3,5 +3,110 @@ namespace aoc2022 { +struct heightmap { + static const int row = 41; + static const int col = 66; + + struct pos { + int x; + int y; + + friend bool operator==(pos p1, pos p2) { + return p1.x == p2.x && p1.y == p2.y; + } + }; + + struct height { + char h; + int v = 0; + }; + + height heights[row * col]; + pos start; + pos end; + + char& get(pos p) { + return heights[p.y * col + p.x].h; + } + + int& was(pos p) { + return heights[p.y * col + p.x].v; + } + + bool valid(pos p) { + return p.x >= 0 && p.x < col && p.y >= 0 && p.y < row; + } + + void get_next(pos p, pos next[], 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]) && !was(ps[i])) { + bool b1 = get(ps[i]) == get(p) + 1; + bool b2 = get(ps[i]) <= get(p); + if (b1 || b2) { + next[*n] = ps[i]; + *n += 1; + } + } + } + } + + 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(int steps, pos p, std::vector<int>& paths) { + // printf("%s (%d,%d) %c\n", indent(steps), p.x, p.y, get(p)); + if (p == end) { + paths.push_back(steps); + } + else { + pos next[4]; + int n = 0; + get_next(p, next, &n); + for(int i = 0; i < n; i++) { + was(next[i]) += 1; + find(steps + 1, next[i], paths); + was(next[i]) -= 1; + } + } + } + + void print() { + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + printf("%c", get({j, i})); + } + printf("\n"); + } + } + + void load(int r, line_view lv) { + const char* p = lv.line; + for (int c = 0; c < col; c++) { + get({c, r}) = *(p + c); + if (*(p + c) == 'S') { + start = pos{c, r}; + get({c, r}) = 'a'; + } + if (*(p + c) == 'E') { + end = pos{c, r}; + get({c, r}) = 'z'; + } + // printf("(%d, %d) %c\n", c, r, get({c, r})); + } + } +}; + std::pair<int, int> day12(line_view); } diff --git a/src/2022/day12/input b/src/2022/day12/input index e69de29..f73c638 100644 --- a/src/2022/day12/input +++ b/src/2022/day12/input @@ -0,0 +1,41 @@ +abcccccccaaaaaccccaaaaaaaccccccccccccccccccccccccccccccccccccaaaaa +abaacccaaaaaaccccccaaaaaaaaaaaaaccccccccccccccccccccccccccccaaaaaa +abaacccaaaaaaaccccaaaaaaaaaaaaaacccccccccccccaacccccccccccccaaaaaa +abaacccccaaaaaacaaaaaaaaaaaaaaaacccccccccccccaacccccccccccccacacaa +abaccccccaaccaacaaaaaaaaaacccaacccccccccccccaaacccccccccccccccccaa +abcccccccaaaacccaaaaaaaaacccccccccccccaaacccaaacccccccccccccccccaa +abccccccccaaaccccccccaaaacccccccccccccaaaaacaaaccacacccccccccccccc +abccccccccaaacaaacccccaaacccccccccccccaaaaaaajjjjjkkkcccccaacccccc +abcccccaaaaaaaaaacccccaaccccccccccciiiiiijjjjjjjjjkkkcaaaaaacccccc +abcccccaaaaaaaaacccccccccccccccccciiiiiiijjjjjjjrrkkkkaaaaaaaacccc +abcccccccaaaaaccccccccccccccccccciiiiiiiijjjjrrrrrppkkkaaaaaaacccc +abcccaaccaaaaaacccccccccccaacaaciiiiqqqqqrrrrrrrrpppkkkaaaaaaacccc +abccaaaaaaaaaaaaccccacccccaaaaaciiiqqqqqqrrrrrruuppppkkaaaaacccccc +abcccaaaaaaacaaaacaaacccccaaaaaahiiqqqqtttrrruuuuupppkkaaaaacccccc +abcaaaaaaaccccaaaaaaacccccaaaaaahhqqqtttttuuuuuuuuuppkkkccaacccccc +abcaaaaaaaaccccaaaaaacccccaaaaaahhqqqtttttuuuuxxuuuppkklcccccccccc +abcaaaaaaaacaaaaaaaaaaacccccaaachhhqqtttxxxuuxxyyuuppllllccccccccc +abcccaaacaccaaaaaaaaaaaccccccccchhhqqtttxxxxxxxyuupppplllccccccccc +abaacaacccccaaaaaaaaaaaccccccccchhhqqtttxxxxxxyyvvvpppplllcccccccc +abaacccccccccaaaaaaacccccccccccchhhpppttxxxxxyyyvvvvpqqqlllccccccc +SbaaccccccaaaaaaaaaaccccccccccchhhppptttxxxEzzyyyyvvvqqqlllccccccc +abaaaaccccaaaaaaaaacccccccccccchhhpppsssxxxyyyyyyyyvvvqqqlllcccccc +abaaaacccccaaaaaaaacccccccccccgggpppsssxxyyyyyyyyyvvvvqqqlllcccccc +abaaacccaaaacaaaaaaaccccccccccgggpppsswwwwwwyyyvvvvvvqqqllllcccccc +abaaccccaaaacaaccaaaacccccccccgggppssswwwwwwyyywvvvvqqqqmmmccccccc +abaaccccaaaacaaccaaaaccaaaccccggpppssssswwswwyywvqqqqqqmmmmccccccc +abcccccccaaacccccaaacccaaacaccgggpppssssssswwwwwwrqqmmmmmccccccccc +abcccccccccccccccccccaacaaaaacgggppooosssssrwwwwrrrmmmmmcccccccccc +abcccccccccccccccccccaaaaaaaacggggoooooooorrrwwwrrnmmmdddccaaccccc +abaccccccccccccaacccccaaaaaccccggggoooooooorrrrrrrnmmddddcaaaccccc +abaccccccccaaaaaaccccccaaaaaccccggfffffooooorrrrrnnndddddaaaaccccc +abaacccccccaaaaaacccccaaaaaacccccffffffffoonrrrrrnnndddaaaaaaacccc +abaaccccccccaaaaaaaccacaaaacccccccccffffffonnnnnnnndddaaaaaaaacccc +abccccccccccaaaaaaaaaaaaaaaccccccccccccfffennnnnnnddddccaaaccccccc +abcccccccccaaaaaaacaaaaaaaaaacccccccccccffeennnnnedddccccaaccccccc +abcccccccccaaaaaaccaaaaaaaaaaaccccccccccaeeeeeeeeeedcccccccccccccc +abccccccccccccaaaccaaaaaaaaaaaccccccccccaaaeeeeeeeecccccccccccccaa +abcccccccaaccccccccaaaaaaaacccccccccccccaaaceeeeecccccccccccccccaa +abaaccaaaaaaccccccccaaaaaaaacccccccccccccaccccaaacccccccccccaaacaa +abaaccaaaaacccccaaaaaaaaaaacccccccccccccccccccccacccccccccccaaaaaa +abaccaaaaaaaaccaaaaaaaaaaaaaacccccccccccccccccccccccccccccccaaaaaa diff --git a/src/2022/day12/input0 b/src/2022/day12/input0 index e69de29..86e9cac 100644 --- a/src/2022/day12/input0 +++ b/src/2022/day12/input0 @@ -0,0 +1,5 @@ +Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi |