aoc-2023/day_8/getlength.ts

50 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2024-12-09 22:41:02 +09:00
const obj = JSON.parse(await Deno.readTextFile("s.json")) as {
prev: string,
name: string
}[];
const nextTable = new Map<string, string>();
for (const { prev, name } of obj) {
nextTable.set(prev, name);
}
function main(start: string) {
let ptr1 = start;
let ptr2 = nextTable.get(start)!;
const path = [] as string[];
while (ptr1 != ptr2) {
path.push(ptr1);
ptr1 = nextTable.get(ptr1)!;
ptr2 = nextTable.get(ptr2)!;
ptr2 = nextTable.get(ptr2)!;
}
path.push(ptr1);
const ringNode = [ptr1];
ptr1 = nextTable.get(ptr1)!;
while (ptr1 != ptr2) {
ringNode.push(ptr1);
ptr1 = nextTable.get(ptr1)!;
}
console.log(ringNode.length, ringNode);
path.forEach((x, i) => {
if (ringNode.includes(x)) {
console.log(`%c${i - 1} %c${x}`, x.endsWith("Z") ? "color: red" : "color: white","color: green");
}
else {
console.log(x);
}
});
}
["AAA","PSA", "GHA", "FXA", "LBA", "HVA"].forEach(x => {
main(x);
})
//63,568,204,859
// 5,247,000,000