From 07cb5d600ab821237db07fff109ac7c5e640aa88 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 9 Dec 2022 10:45:26 -0500 Subject: Solve day 9 part 1 --- 2022/day09.awk | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 2022/day09.awk (limited to '2022/day09.awk') 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; +} -- cgit 1.4.1