diff options
Diffstat (limited to 'src/2022/day8/aoc.h')
-rw-r--r-- | src/2022/day8/aoc.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/2022/day8/aoc.h b/src/2022/day8/aoc.h index 23b9c63..b3f5217 100644 --- a/src/2022/day8/aoc.h +++ b/src/2022/day8/aoc.h @@ -1,6 +1,98 @@ #include "common.h" namespace aoc2022 { +struct trees { + static const int grid = 99; + + struct pos { + int x; + int y; + }; + + char ts[grid * grid] = {0}; + + void load(int r, line_view lv) { + for(int i = 0; i < grid; i++) { + ts[r * grid + i] = *(lv.line + i) - '0'; + // printf("%d %d -> %d\n", i, r, ts[r * grid + i]); + } + } + + void print() { + for(int y = 0; y < grid; y++) { + for (int x = 0; x < grid; x++) { + printf("%d", height({x, y})); + } + printf("\n"); + } + } + + enum direction { + top, + left, + bottom, + right, + }; + + int height(pos p) { + return ts[p.y* grid + p.x]; + } + + bool valid(pos p) { + return p.x < grid && p.x >= 0 && p.y < grid && p.y >= 0; + } + + pos next(direction d, pos p) { + switch (d) { + case top : return {p.x, p.y - 1}; + case left : return {p.x - 1, p.y}; + case bottom : return {p.x, p.y + 1}; + case right : return {p.x + 1, p.y}; + } + return {-1, -1}; + } + + int score(pos p) { + direction ds[4] = {top, left, bottom, right}; + int s[4] = {0, 0, 0, 0}; + for (int i = 0; i < 4; i++) { + auto x = next(ds[i], p); + while(valid(x)) { + s[i] += 1; + if (height(p) <= height(x)) { + break; + } + x = next(ds[i], x); + } + } + // printf("[%d, %d](%d) has score [%d, %d, %d, %d]\n", + // p.x, p.y, height(p), s[0], s[1], s[2], s[3]); + return s[0] * s[1] * s[2] * s[3]; + } + + bool visiable(pos p) { + direction ds[4] = {top, left, bottom, right}; + bool visiable[4] = {true, true, true, true}; + // const char* literal[4] = {"top", "left", "bottom", "right"}; + for (int i = 0; i < 4; i++) { + auto x = next(ds[i], p); + while(valid(x)) { + if (height(x) >= height(p)) { + // printf("(%d,%d) %d is not visiable from [%s]\n", p.x, p.y, height(p), literal[i]); + visiable[i] = false; + break; + } + x = next(ds[i], x); + } + } + for (bool b: visiable) { + if (b) return true; + } + return false; + } + +}; + std::pair<int,int> day8(line_view file); } |