summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-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;
+}