diff options
author | June McEnroe <june@causal.agency> | 2022-12-09 11:12:22 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2022-12-09 11:12:22 -0500 |
commit | 790b8e34f92680e4d0b52f2fbcb5654420c20caf (patch) | |
tree | 8631ddf265d875ea16d6e817034d62dc51498f4b /2022 | |
parent | Solve day 9 part 1 (diff) | |
download | aoc-790b8e34f92680e4d0b52f2fbcb5654420c20caf.tar.gz aoc-790b8e34f92680e4d0b52f2fbcb5654420c20caf.zip |
Solve day 9 part 2
Diffstat (limited to '2022')
-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; } |