summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-12-08 13:01:41 -0500
committerJune McEnroe <june@causal.agency>2022-12-08 13:01:41 -0500
commit2ecc5db82d92d7a6fffbe7f8c1f318583a80ea24 (patch)
tree37e4e3f43cc8b2294d0b13ba894d050114065143
parentSolve day 7 part 2 (diff)
downloadaoc-2ecc5db82d92d7a6fffbe7f8c1f318583a80ea24.tar.gz
aoc-2ecc5db82d92d7a6fffbe7f8c1f318583a80ea24.zip
Solve day 8 part 1
Diffstat (limited to '')
-rw-r--r--2022/day08.awk39
1 files changed, 39 insertions, 0 deletions
diff --git a/2022/day08.awk b/2022/day08.awk
new file mode 100644
index 0000000..35505e4
--- /dev/null
+++ b/2022/day08.awk
@@ -0,0 +1,39 @@
+function visible(x, y) {
+	if (x == 1 || x == right) return 1;
+	if (y == 1 || y == bottom) return 1;
+	for (xx = x-1; xx >= 1; --xx) {
+		if (m[xx,y] >= m[x,y]) break;
+	}
+	if (!xx) return 1;
+	for (xx = x+1; xx <= right; ++xx) {
+		if (m[xx,y] >= m[x,y]) break;
+	}
+	if (xx > right) return 1;
+	for (yy = y-1; yy >= 1; --yy) {
+		if (m[x,yy] >= m[x,y]) break;
+	}
+	if (!yy) return 1;
+	for (yy = y+1; yy <= bottom; ++yy) {
+		if (m[x,yy] >= m[x,y]) break;
+	}
+	if (yy > bottom) return 1;
+	return 0;
+}
+BEGIN {
+	FS = "";
+}
+{
+	for (x = 1; x <= NF; ++x) {
+		m[x,NR] = $x;
+	}
+	right = NF;
+	bottom = NR;
+}
+END {
+	for (x = 1; x <= right; ++x) {
+		for (y = 1; y <= bottom; ++y) {
+			if (visible(x, y)) vis++;
+		}
+	}
+	print vis;
+}