summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-12-09 11:12:22 -0500
committerJune McEnroe <june@causal.agency>2022-12-09 11:12:22 -0500
commit790b8e34f92680e4d0b52f2fbcb5654420c20caf (patch)
tree8631ddf265d875ea16d6e817034d62dc51498f4b
parentSolve day 9 part 1 (diff)
downloadaoc-790b8e34f92680e4d0b52f2fbcb5654420c20caf.tar.gz
aoc-790b8e34f92680e4d0b52f2fbcb5654420c20caf.zip
Solve day 9 part 2
Diffstat (limited to '')
-rw-r--r--2022/day09.awk41
1 files changed, 24 insertions, 17 deletions
diff --git a/2022/day09.awk b/2022/day09.awk
index 95e4fb4..da142a4 100644
--- a/2022/day09.awk
+++ b/2022/day09.awk
@@ -1,23 +1,22 @@
 BEGIN {
-	t[0,0] = 1;
+	t1[0,0] = 1;
+	t9[0,0] = 1;
 }
 function abs(x) {
-	if (x < 0) return -x;
-	return x;
+	return (x < 0 ? -x : x);
 }
-function step() {
-	rx = tx-hx;
-	ry = ty-hy;
+function step(k) {
+	rx = kx[k] - kx[k-1];
+	ry = ky[k] - ky[k-1];
 	if (abs(rx) <= 1 && abs(ry) <= 1) return;
 	if (rx == 0) {
-		ty += (ry < 0 ? +1 : -1);
+		ky[k] += (ry < 0 ? +1 : -1);
 	} else if (ry == 0) {
-		tx += (rx < 0 ? +1 : -1);
+		kx[k] += (rx < 0 ? +1 : -1);
 	} else {
-		tx += (rx < 0 ? +1 : -1);
-		ty += (ry < 0 ? +1 : -1);
+		kx[k] += (rx < 0 ? +1 : -1);
+		ky[k] += (ry < 0 ? +1 : -1);
 	}
-	t[tx,ty] = 1;
 }
 {
 	dx = 0;
@@ -27,14 +26,22 @@ function step() {
 	if ($1 == "L") dx = -1;
 	if ($1 == "R") dx = +1;
 	for (i = 1; i <= $2; ++i) {
-		hx += dx;
-		hy += dy;
-		step();
+		kx[0] += dx;
+		ky[0] += dy;
+		for (k = 1; k <= 9; ++k) {
+			step(k);
+		}
+		t1[+kx[1],+ky[1]] = 1;
+		t9[+kx[9],+ky[9]] = 1;
 	}
 }
 END {
-	for (i in t) {
-		sum++;
+	for (i in t1) {
+		sum1++;
 	}
-	print sum;
+	print sum1;
+	for (i in t9) {
+		sum9++;
+	}
+	print sum9;
 }
gmsg'> Git upstream bans strncpy() with commit: banned.h: mark strncpy() as banned e488b7aba743d23b830d239dcc33d9ca0745a9ad Signed-off-by: Christian Hesse <mail@eworm.de> 2018-09-11ui-log: ban strcpy()Christian Hesse Git upstream bans strcpy() with commit: automatically ban strcpy() c8af66ab8ad7cd78557f0f9f5ef6a52fd46ee6dd Signed-off-by: Christian Hesse <mail@eworm.de> 2018-09-11parsing: ban sprintf()Christian Hesse Git upstream bans sprintf() with commit: banned.h: mark sprintf() as banned cc8fdaee1eeaf05d8dd55ff11f111b815f673c58 Signed-off-by: Christian Hesse <mail@eworm.de> 2018-09-11parsing: ban strncpy()Christian Hesse Git upstream bans strncpy() with commit: banned.h: mark strncpy() as banned e488b7aba743d23b830d239dcc33d9ca0745a9ad Signed-off-by: Christian Hesse <mail@eworm.de> 2018-08-28filters: generate anchor links from markdownChristian Hesse This makes the markdown filter generate anchor links for headings. Signed-off-by: Christian Hesse <mail@eworm.de> Tested-by: jean-christophe manciot <actionmystique@gmail.com> 2018-08-03Bump version.Jason A. Donenfeld Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> 2018-08-03clone: fix directory traversalJason A. Donenfeld This was introduced in the initial version of this code, way back when in 2008. $ curl http://127.0.0.1/cgit/repo/objects/?path=../../../../../../../../../etc/passwd root:x:0:0:root:/root:/bin/sh ... Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Reported-by: Jann Horn <jannh@google.com> 2018-08-03config: record repo.snapshot-prefix in the per-repo configKonstantin Ryabitsev