diff options
author | dzwdz | 2022-12-12 18:50:39 +0100 |
---|---|---|
committer | dzwdz | 2022-12-12 18:50:39 +0100 |
commit | 26dd7e867ab392771b4b9a095ac409a18b5e7a71 (patch) | |
tree | 7d98342667aa6ca196b5fd300f0df4a7db512e52 /22.12 | |
parent | c91c6347a0a63a7f14e12677be181546880d16ce (diff) |
day 12 part 2
Diffstat (limited to '22.12')
-rw-r--r-- | 22.12/main.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/22.12/main.cpp b/22.12/main.cpp index 110cf15..5c7519e 100644 --- a/22.12/main.cpp +++ b/22.12/main.cpp @@ -74,8 +74,40 @@ part1() } int +part2() +{ + bool visited[h][w] = {0}; + deque<Point> frontier; + frontier.push_back(goal); + + while (!frontier.empty()) { + Point p = frontier[0]; + frontier.pop_front(); + for (auto n : { + Point{p.x + 1, p.y , p.v + 1}, + Point{p.x - 1, p.y , p.v + 1}, + Point{p.x , p.y + 1, p.v + 1}, + Point{p.x , p.y - 1, p.v + 1}, + }) { + if (!(0 <= n.x && n.x < w && 0 <= n.y && n.y < h)) + continue; + if (visited[n.y][n.x]) continue; + auto h1 = heights[p.y][p.x]; + auto h2 = heights[n.y][n.x]; + if (h2 + 1 < h1) continue; + visited[n.y][n.x] = true; + if (h2 == 0) return n.v; + frontier.push_back(n); + // cout << n.x << " " << n.y << " " << n.v << endl; + } + } + return -1; +} + +int main() { readInput(); cout << part1() << endl; + cout << part2() << endl; } |