summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2021/day09.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/2021/day09.c b/2021/day09.c
new file mode 100644
index 0000000..44c20a1
--- /dev/null
+++ b/2021/day09.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <stdlib.h>
+static int h, w;
+static int grid[128][128];
+static int adj(int *a, int y, int x) {
+	int n = 0;
+	if (y > 0) a[n++] = grid[y-1][x];
+	if (y < h-1) a[n++] = grid[y+1][x];
+	if (x > 0) a[n++] = grid[y][x-1];
+	if (x < w-1) a[n++] = grid[y][x+1];
+	return n;
+}
+int main(void) {
+	size_t cap = 0;
+	char *buf = NULL;
+	while (0 < getline(&buf, &cap, stdin)) {
+		for (w = 0; buf[w] && buf[w] != '\n'; ++w) {
+			grid[h][w] = buf[w] - '0';
+		}
+		h++;
+	}
+	int risk = 0;
+	for (int y = 0; y < h; ++y)
+	for (int x = 0; x < w; ++x) {
+		int a[4];
+		int n = adj(a, y, x);
+		int m = 0;
+		for (int i = 0; i < n; ++i) {
+			if (a[i] <= grid[y][x]) m |= 1;
+		}
+		if (!m) risk += 1 + grid[y][x];
+	}
+	printf("%d\n", risk);
+}
rl/commit/catgirl.1?h=1.4&id=1d6fb01074e41cd30e83c7587942fff9605c8b2d&follow=1'>Clean up man pageJune McEnroe 2019-01-26Draw UI before connectingJune McEnroe 2019-01-25Avoid unused variable warnings with getyxJune McEnroe 2019-01-25Add GNU/Linux build instructionsJune McEnroe