summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-12-09 12:42:38 -0500
committerJune McEnroe <june@causal.agency>2021-12-09 12:42:38 -0500
commit44ad339f8f8e8b578b151938c4abf080678b30bb (patch)
treef0adfaa4bd1b4cc235649da1b6504aaee35c8879
parentSolve day 8 part 2 (diff)
downloadaoc-44ad339f8f8e8b578b151938c4abf080678b30bb.tar.gz
aoc-44ad339f8f8e8b578b151938c4abf080678b30bb.zip
Solve day 9 part 1
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);
+}