summary refs log tree commit diff homepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2022/day09.awk40
1 files changed, 40 insertions, 0 deletions
diff --git a/2022/day09.awk b/2022/day09.awk
new file mode 100644
index 0000000..95e4fb4
--- /dev/null
+++ b/2022/day09.awk
@@ -0,0 +1,40 @@
+BEGIN {
+	t[0,0] = 1;
+}
+function abs(x) {
+	if (x < 0) return -x;
+	return x;
+}
+function step() {
+	rx = tx-hx;
+	ry = ty-hy;
+	if (abs(rx) <= 1 && abs(ry) <= 1) return;
+	if (rx == 0) {
+		ty += (ry < 0 ? +1 : -1);
+	} else if (ry == 0) {
+		tx += (rx < 0 ? +1 : -1);
+	} else {
+		tx += (rx < 0 ? +1 : -1);
+		ty += (ry < 0 ? +1 : -1);
+	}
+	t[tx,ty] = 1;
+}
+{
+	dx = 0;
+	dy = 0;
+	if ($1 == "U") dy = -1;
+	if ($1 == "D") dy = +1;
+	if ($1 == "L") dx = -1;
+	if ($1 == "R") dx = +1;
+	for (i = 1; i <= $2; ++i) {
+		hx += dx;
+		hy += dy;
+		step();
+	}
+}
+END {
+	for (i in t) {
+		sum++;
+	}
+	print sum;
+}