summary refs log tree commit diff homepage
path: root/2021/day09.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-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);
+}