aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day12
diff options
context:
space:
mode:
Diffstat (limited to 'src/2022/day12')
-rw-r--r--src/2022/day12/aoc.cpp8
-rw-r--r--src/2022/day12/aoc.h51
2 files changed, 25 insertions, 34 deletions
diff --git a/src/2022/day12/aoc.cpp b/src/2022/day12/aoc.cpp
index 7b79e94..4cc388d 100644
--- a/src/2022/day12/aoc.cpp
+++ b/src/2022/day12/aoc.cpp
@@ -12,11 +12,11 @@ std::pair<int, int> day12(line_view file) {
});
// hm.print();
- std::vector<heightmap::pos> ps;
- hm.find_prev(0, hm.end, ps);
+ std::vector<int> steps;
+ hm.find(hm.end, steps);
- for (auto &p : ps) {
- printf("%d %d\n", p.x, p.y);
+ for(auto& i : steps) {
+ printf("%d\n", i);
}
return {0, 0};
}
diff --git a/src/2022/day12/aoc.h b/src/2022/day12/aoc.h
index 3ac0c76..a7380b2 100644
--- a/src/2022/day12/aoc.h
+++ b/src/2022/day12/aoc.h
@@ -1,5 +1,6 @@
#include "common.h"
#include <vector>
+#include <deque>
namespace aoc2022 {
@@ -84,36 +85,26 @@ struct heightmap {
return space;
};
- void find_prev(int steps, pos p, std::vector<pos>& as) {
- // printf("%s (%d,%d) %c\n", indent(steps), p.x, p.y, get(p));
- if (get(p) == 'a') {
- as.push_back(p);
- }
- else {
- pos prev[4];
- int n = 0;
- get_prev(p, prev, &n);
- for (int i = 0; i < n; i++) {
- find_prev(steps + 1, prev[i], as);
- }
- }
- }
-
- 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 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() {