answer2 = {
const root = { v: 0, c: [], a: 0 };
const nodeMap = new Map();
for (const v of input) {
nodeMap.set(v, { v, c: [], a: 0 });
}
for (let i = 1; i <= 3; i++) {
const child = nodeMap.get(i);
if (child) root.c.push(child);
}
for (const [v, node] of nodeMap) {
for (let i = v + 1; i <= v + 3; i++) {
const child = nodeMap.get(i);
if (child) node.c.push(child);
}
}
const maxJolt = input[input.length - 1];
const arrangements = node => {
if (node.a !== 0) return node.a;
if (node.v === maxJolt) {
node.a = 1;
return 1;
}
for (const child of node.c) {
node.a += arrangements(child);
}
return node.a;
};
arrangements(root);
return { arrangements: root.a, root, nodeMap };
}