this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

part 2, logic is right, but running too slow

+89 -8
+29 -2
2023/day08/part2.ts
··· 1 + type Node = { L: string; R: string }; 2 + type Network = Record<string, Node>; 3 + 1 4 export function answer(input: string): number { 2 - console.log(input); 3 - return 42; 5 + const [instructions, , ...networkRaw] = input.split("\n"); 6 + 7 + const network: Network = networkRaw.reduce((network, line) => { 8 + const [id, leftAndRight] = line.split(" = "); 9 + const [L, R] = leftAndRight.replaceAll(/[\(\)]/g, "").split(", "); 10 + return { ...network, [id]: { L, R } }; 11 + }, {}); 12 + 13 + const pathsFinished = (paths: string[]): boolean => 14 + paths.every((p) => p.endsWith("Z")); 15 + 16 + const step = (paths: string[], dir: keyof Node): string[] => 17 + paths.map((c) => network[c][dir]); 18 + 19 + let paths = Object.keys(network).filter((n) => n.endsWith("A")); 20 + let stepCount = 0; 21 + 22 + while (!pathsFinished(paths)) { 23 + for (const dir of instructions.split("")) { 24 + paths = step(paths, dir as keyof Node); 25 + stepCount++; 26 + if (pathsFinished(paths)) break; 27 + } 28 + } 29 + 30 + return stepCount; 4 31 } 5 32 6 33 if (import.meta.main) {
+45 -2
2023/day08/puzzle.md
··· 39 39 40 40 Starting at `AAA`, follow the left/right instructions. *How many steps are required to reach `ZZZ`?* 41 41 42 - To begin, [get your puzzle input](8/input). 42 + Your puzzle answer was `16697`. 43 + 44 + The first half of this puzzle is complete! It provides one gold star: \* 45 + 46 + \--- Part Two --- 47 + ---------- 48 + 49 + 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! 50 + 51 + 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. 52 + 53 + 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`. 54 + 55 + For example: 56 + 57 + ``` 58 + LR 59 + 60 + 11A = (11B, XXX) 61 + 11B = (XXX, 11Z) 62 + 11Z = (11B, XXX) 63 + 22A = (22B, XXX) 64 + 22B = (22C, 22C) 65 + 22C = (22Z, 22Z) 66 + 22Z = (22B, 22B) 67 + XXX = (XXX, XXX) 68 + 69 + ``` 70 + 71 + 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: 72 + 73 + * Step 0: You are at `11A` and `22A`. 74 + * Step 1: You choose all of the *left* paths, leading you to `11B` and `22B`. 75 + * Step 2: You choose all of the *right* paths, leading you to `*11Z*` and `22C`. 76 + * Step 3: You choose all of the *left* paths, leading you to `11B` and `*22Z*`. 77 + * Step 4: You choose all of the *right* paths, leading you to `*11Z*` and `22B`. 78 + * Step 5: You choose all of the *left* paths, leading you to `11B` and `22C`. 79 + * Step 6: You choose all of the *right* paths, leading you to `*11Z*` and `*22Z*`. 80 + 81 + So, in this example, you end up entirely on nodes that end in `Z` after `*6*` steps. 82 + 83 + 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`?* 43 84 44 85 Answer: 45 86 46 - 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. 87 + Although it hasn't changed, you can still [get your puzzle input](8/input). 88 + 89 + 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.
+15 -4
2023/day08/test.ts
··· 28 28 assertEquals(p1.answer(examples2), 6); 29 29 }); 30 30 31 - // Deno.test("part2", () => { 32 - // const examples = ["abc", "def"].join("\n"); 33 - // assertEquals(p2.answer(examples), 42); 34 - // }); 31 + Deno.test("part2", () => { 32 + const examples = [ 33 + "LR", 34 + "", 35 + "11A = (11B, XXX)", 36 + "11B = (XXX, 11Z)", 37 + "11Z = (11B, XXX)", 38 + "22A = (22B, XXX)", 39 + "22B = (22C, 22C)", 40 + "22C = (22Z, 22Z)", 41 + "22Z = (22B, 22B)", 42 + "XXX = (XXX, XXX)", 43 + ].join("\n"); 44 + assertEquals(p2.answer(examples), 6); 45 + });