diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-12-02 14:16:56 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-12-02 14:16:56 -0700 |
commit | cfd970e21663c3278f6e01d356690789225b6b56 (patch) | |
tree | a868659cc8425cc47ba1cd02509b3fe80632039c /aoc_2022/day-09/part_2.ts | |
parent | 118fc144884f0d716cc03e877aa85f83d289cec8 (diff) | |
download | aoc-cfd970e21663c3278f6e01d356690789225b6b56.tar.gz aoc-cfd970e21663c3278f6e01d356690789225b6b56.zip |
2022 day 9 and add utils
Diffstat (limited to 'aoc_2022/day-09/part_2.ts')
-rw-r--r-- | aoc_2022/day-09/part_2.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/aoc_2022/day-09/part_2.ts b/aoc_2022/day-09/part_2.ts new file mode 100644 index 0000000..5b0df2a --- /dev/null +++ b/aoc_2022/day-09/part_2.ts @@ -0,0 +1,61 @@ +import { JSONSet, Vec2, l2Norm, isDiagAdj } from "@/utils"; + +export const main = async (lines: string[]): Promise<number | string> => { + const knots: Vec2[] = Array(10) + .fill(null) + .map(() => ({ x: 0, y: 0 })); + + const visited: JSONSet<Vec2> = new JSONSet(); + visited.add(knots[0]); + + for (const step of lines) { + const [dir, steps_s] = step.split(" "); + const steps = parseInt(steps_s); + + for (let i = 0; i < steps; ++i) { + if (dir === "U") knots[0].y += 1; + if (dir === "L") knots[0].x -= 1; + if (dir === "R") knots[0].x += 1; + if (dir === "D") knots[0].y -= 1; + + for (let knotidx = 1; knotidx < knots.length; knotidx++) { + const [head, tail] = [knots[knotidx - 1], knots[knotidx]]; + + if (!isDiagAdj(head, tail)) { + if (head.y - tail.y === 0) { + tail.x += Math.floor((head.x - tail.x) / 2); + } else if (head.x - tail.x === 0) { + tail.y += Math.floor((head.y - tail.y) / 2); + } else { + tail.x += head.x - tail.x > 0 ? 1 : -1; + tail.y += head.y - tail.y > 0 ? 1 : -1; + } + } + + if (knotidx === knots.length - 1) { + visited.add(tail); + } + } + } + } + + return visited.size; +}; + +// + +const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path; +if (isrun) { + const file = Bun.file("./problem.txt"); + const text = await file.text(); + const lines = text.split("\n").filter((x) => x && x.length); + + console.log("=== COMPUTATION ===\n"); + + const answer = await main(lines); + + console.log("\n=== /COMPUTATION ===\n"); + + console.log("=== ANSWER TO P2 ==="); + console.log(answer); +} |