From 790b8e34f92680e4d0b52f2fbcb5654420c20caf Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 9 Dec 2022 11:12:22 -0500 Subject: Solve day 9 part 2 --- 2022/day09.awk | 41 ++++++++++++++++++++++++----------------- 1 file 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; } -- cgit 1.4.1