diff options
Diffstat (limited to '')
-rw-r--r-- | 2022/day09.awk | 41 |
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; } |