aboutsummaryrefslogtreecommitdiff
path: root/src/2021/day9/aoc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/2021/day9/aoc.h')
-rw-r--r--src/2021/day9/aoc.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/2021/day9/aoc.h b/src/2021/day9/aoc.h
index cd205cd..5e079fe 100644
--- a/src/2021/day9/aoc.h
+++ b/src/2021/day9/aoc.h
@@ -3,4 +3,40 @@
namespace aoc2021 {
+struct heightmap {
+ // 0 .. 99
+ char hs[100 * 100] = {0};
+
+ void load(int r, line_view lv) {
+ const char* p = lv.line;
+ for(int i = 0; i < 100; i++) {
+ hs[100 * r + i] = *(p + i) - '0';
+ }
+ }
+
+ void low_point(int x, int y, int* d) const noexcept {
+ char l = x == 0 ? 10 : hs[100 * y + x - 1];
+ char r = x == 99 ? 10 : hs[100 * y + x + 1];
+ char t = y == 0 ? 10 : hs[100 * (y - 1) + x];
+ char b = y == 99 ? 10 : hs[100 * (y + 1) + x];
+
+ char n = hs[100 * y + x];
+ if (n < l && n < r && n < t && n < b) {
+ *d += n + 1;
+ }
+ }
+
+ int risks() const noexcept {
+ int r{0};
+ for (int y = 0; y < 100; y++) {
+ for (int x = 0; x < 100; x++) {
+ low_point(x, y, &r);
+ }
+ }
+ return r;
+ }
+};
+
+std::pair<int, int> day9(line_view file);
+
}