diff --git a/2023/day08/part2.ts b/2023/day08/part2.ts index 3692f70..7527f84 100644 --- a/2023/day08/part2.ts +++ b/2023/day08/part2.ts @@ -1,6 +1,33 @@ +type Node = { L: string; R: string }; +type Network = Record; + export function answer(input: string): number { - console.log(input); - return 42; + const [instructions, , ...networkRaw] = input.split("\n"); + + const network: Network = networkRaw.reduce((network, line) => { + const [id, leftAndRight] = line.split(" = "); + const [L, R] = leftAndRight.replaceAll(/[\(\)]/g, "").split(", "); + return { ...network, [id]: { L, R } }; + }, {}); + + const pathsFinished = (paths: string[]): boolean => + paths.every((p) => p.endsWith("Z")); + + const step = (paths: string[], dir: keyof Node): string[] => + paths.map((c) => network[c][dir]); + + let paths = Object.keys(network).filter((n) => n.endsWith("A")); + let stepCount = 0; + + while (!pathsFinished(paths)) { + for (const dir of instructions.split("")) { + paths = step(paths, dir as keyof Node); + stepCount++; + if (pathsFinished(paths)) break; + } + } + + return stepCount; } if (import.meta.main) { diff --git a/2023/day08/puzzle.md b/2023/day08/puzzle.md index 0fd61c0..45c1722 100644 --- a/2023/day08/puzzle.md +++ b/2023/day08/puzzle.md @@ -39,8 +39,51 @@ ZZZ = (ZZZ, ZZZ) Starting at `AAA`, follow the left/right instructions. *How many steps are required to reach `ZZZ`?* -To begin, [get your puzzle input](8/input). +Your puzzle answer was `16697`. + +The first half of this puzzle is complete! It provides one gold star: \* + +\--- Part Two --- +---------- + +The sandstorm is upon you and you aren't any closer to escaping the wasteland. You had the camel follow the instructions, but you've barely left your starting position. It's going to take *significantly more steps* to escape! + +What if the map isn't for people - what if the map is for *ghosts*? Are ghosts even bound by the laws of spacetime? Only one way to find out. + +After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in `A` is equal to the number ending in `Z`! If you were a ghost, you'd probably just *start at every node that ends with `A`* and follow all of the paths at the same time until they all simultaneously end up at nodes that end with `Z`. + +For example: + +``` +LR + +11A = (11B, XXX) +11B = (XXX, 11Z) +11Z = (11B, XXX) +22A = (22B, XXX) +22B = (22C, 22C) +22C = (22Z, 22Z) +22Z = (22B, 22B) +XXX = (XXX, XXX) + +``` + +Here, there are two starting nodes, `11A` and `22A` (because they both end with `A`). As you follow each left/right instruction, use that instruction to *simultaneously* navigate away from both nodes you're currently on. Repeat this process until *all* of the nodes you're currently on end with `Z`. (If only some of the nodes you're on end with `Z`, they act like any other node and you continue as normal.) In this example, you would proceed as follows: + +* Step 0: You are at `11A` and `22A`. +* Step 1: You choose all of the *left* paths, leading you to `11B` and `22B`. +* Step 2: You choose all of the *right* paths, leading you to `*11Z*` and `22C`. +* Step 3: You choose all of the *left* paths, leading you to `11B` and `*22Z*`. +* Step 4: You choose all of the *right* paths, leading you to `*11Z*` and `22B`. +* Step 5: You choose all of the *left* paths, leading you to `11B` and `22C`. +* Step 6: You choose all of the *right* paths, leading you to `*11Z*` and `*22Z*`. + +So, in this example, you end up entirely on nodes that end in `Z` after `*6*` steps. + +Simultaneously start on every node that ends with `A`. *How many steps does it take before you're only on nodes that end with `Z`?* Answer: -You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Haunted+Wasteland%22+%2D+Day+8+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F8&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file +Although it hasn't changed, you can still [get your puzzle input](8/input). + +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Haunted+Wasteland%22+%2D+Day+8+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F8&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. \ No newline at end of file diff --git a/2023/day08/test.ts b/2023/day08/test.ts index 60ef0fd..5ced77b 100644 --- a/2023/day08/test.ts +++ b/2023/day08/test.ts @@ -28,7 +28,18 @@ Deno.test("part1", () => { assertEquals(p1.answer(examples2), 6); }); -// Deno.test("part2", () => { -// const examples = ["abc", "def"].join("\n"); -// assertEquals(p2.answer(examples), 42); -// }); +Deno.test("part2", () => { + const examples = [ + "LR", + "", + "11A = (11B, XXX)", + "11B = (XXX, 11Z)", + "11Z = (11B, XXX)", + "22A = (22B, XXX)", + "22B = (22C, 22C)", + "22C = (22Z, 22Z)", + "22Z = (22B, 22B)", + "XXX = (XXX, XXX)", + ].join("\n"); + assertEquals(p2.answer(examples), 6); +});