aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day12/aoc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/2022/day12/aoc.h')
-rw-r--r--src/2022/day12/aoc.h75
1 files changed, 26 insertions, 49 deletions
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}));
}
}
};