this repo has no description
0
fork

Configure Feed

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

part 1!

+43 -3
+29 -2
2023/day08/part1.ts
··· 1 + type Node = { 2 + left: string; 3 + right: string; 4 + }; 5 + 6 + type Network = Record<string, Node>; 7 + 1 8 export function answer(input: string): number { 2 - console.log(input); 3 - return 42; 9 + const [instructions, , ...networkRaw] = input.split("\n"); 10 + 11 + const network: Network = networkRaw.reduce((network, line) => { 12 + const [id, leftAndRight] = line.split(" = "); 13 + const [left, right] = leftAndRight.replaceAll(/[\(\)]/g, "").split(", "); 14 + return { 15 + ...network, 16 + [id]: { left: left, right: right }, 17 + }; 18 + }, {}); 19 + 20 + let currentNode = "AAA"; 21 + let stepCount = 0; 22 + while (currentNode !== "ZZZ") { 23 + for (const dir of instructions.split("")) { 24 + currentNode = network[currentNode][dir === "L" ? "left" : "right"]; 25 + stepCount++; 26 + if (currentNode === "ZZZ") break; 27 + } 28 + } 29 + 30 + return stepCount; 4 31 } 5 32 6 33 if (import.meta.main) {
+14 -1
2023/day08/test.ts
··· 4 4 5 5 Deno.test("part1", () => { 6 6 const examples = [ 7 + "RL", 8 + "", 7 9 "AAA = (BBB, CCC)", 8 10 "BBB = (DDD, EEE)", 9 11 "CCC = (ZZZ, GGG)", ··· 12 14 "GGG = (GGG, GGG)", 13 15 "ZZZ = (ZZZ, ZZZ)", 14 16 ].join("\n"); 15 - assertEquals(p1.answer(examples), 6); 17 + const examples2 = [ 18 + "LLR", 19 + "", 20 + "AAA = (BBB, BBB)", 21 + "BBB = (AAA, ZZZ)", 22 + "ZZZ = (ZZZ, ZZZ)", 23 + "EEE = (EEE, EEE)", 24 + "GGG = (GGG, GGG)", 25 + "ZZZ = (ZZZ, ZZZ)", 26 + ].join("\n"); 27 + assertEquals(p1.answer(examples), 2); 28 + assertEquals(p1.answer(examples2), 6); 16 29 }); 17 30 18 31 // Deno.test("part2", () => {