summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-12-09 10:45:26 -0500
committerJune McEnroe <june@causal.agency>2022-12-09 10:45:26 -0500
commit07cb5d600ab821237db07fff109ac7c5e640aa88 (patch)
tree4772496fd689147fd5ec84e39f214f082c133f3d
parentSolve day 8 part 2 (diff)
downloadaoc-07cb5d600ab821237db07fff109ac7c5e640aa88.tar.gz
aoc-07cb5d600ab821237db07fff109ac7c5e640aa88.zip
Solve day 9 part 1
-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;
+}