Graph traversal, heck yeah!
For Part 1 everything went swimmingly, I didn’t even ever run the solution in Node, I just ran the Node program that splatted out the input into JSON on the full input
import { Dusa } from './lib/client.cjs';
const INPUT0 = `
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)
`
.trim()
.split('\\n');
const INPUT = {
steps: INPUT0[0].trim().split(''),
graph: INPUT0.slice(2).map((line) => ({
start: line.slice(0, 3),
left: line.slice(7, 10),
right: line.slice(12, 15),
})),
};
const dusa = new Dusa('');
dusa.load(INPUT, 'field');
for (const [ref, field, value] of dusa.solution.lookup('field')) {
console.log(
`field ref${ref.value} ${typeof field === 'string' ? `"${field}"` : field} is ${
typeof value === 'string'
? `"${value}"`
: typeof value === 'bigint'
? value
: `ref${value.value}`
}.`,
);
}
And then ran that (but with the full solution) in the browser.
Here’s the program on dusa.rocks
Part 2 faked me out: I thought it was a problem with Dusa doing stuff efficiently enough, like for Day 7, and tried to do a solution that put the tight loop in JavaScript, avoiding creating new facts for every step taken in Dusa, which just eats memory without end.
If you’ve done this AoC Day 8, you’ll know that wasn’t ever going to work, and actually it would have been quite nice to do the figure-out-how-long-the-loop-is detection in Dusa… but I’m going to say maybe I’ll come back to that, because I ended up just messily solving the problem in JavaScript instead, though using the Dusa program to look up the next move.