summary refs log tree commit diff homepage
path: root/2022
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-12-15 18:06:53 -0500
committerJune McEnroe <june@causal.agency>2022-12-15 18:06:53 -0500
commit543c82574c111066691d8b05931214b5295f0f83 (patch)
tree40f462789f4a9df3c009a0041669d172d78db47d /2022
parentSolve day 12 part 2 (diff)
downloadaoc-543c82574c111066691d8b05931214b5295f0f83.tar.gz
aoc-543c82574c111066691d8b05931214b5295f0f83.zip
Solve day 14 part 1
Diffstat (limited to '')
-rw-r--r--2022/day14.awk44
1 files changed, 44 insertions, 0 deletions
diff --git a/2022/day14.awk b/2022/day14.awk
new file mode 100644
index 0000000..45bc5fe
--- /dev/null
+++ b/2022/day14.awk
@@ -0,0 +1,44 @@
+BEGIN {
+	FS = "[ ,>-]+";
+}
+{
+	x = $1;
+	y = $2;
+	for (i = 3; i <= NF; i += 2) {
+		tx = $i;
+		ty = $(i+1);
+		while (x != tx) {
+			m[x,y] = "#";
+			if (x < tx) x++;
+			if (x > tx) x--;
+		}
+		while (y != ty) {
+			m[x,y] = "#";
+			if (y < ty) y++;
+			if (y > ty) y--;
+		}
+		m[x,y] = "#";
+		if (y > ymax) ymax = y;
+	}
+}
+END {
+	x = 500;
+	y = 0;
+	while (y < ymax) {
+		if (!m[x,y+1]) {
+			y++;
+		} else if (!m[x-1,y+1]) {
+			x--;
+			y++;
+		} else if (!m[x+1,y+1]) {
+			x++;
+			y++;
+		} else {
+			m[x,y] = "o";
+			sand++;
+			x = 500;
+			y = 0;
+		}
+	}
+	print sand;
+}