ghostingCycles = function* (directions, connections) {
let currents = Object.keys(connections).filter(
(con) => con[con.length - 1] == "A"
);
let cycles = currents.map((c) => 0);
let steps = 0;
while (
cycles.filter((loop) => loop == 0).length > 0 &&
currents.filter((con) => con[con.length - 1] != "Z").length > 0
) {
for (let dir of directions) {
currents = currents.map((current, idx) => {
if (
steps % directions.length == 0 &&
current[current.length - 1] == "Z" &&
cycles[idx] == 0
) {
cycles[idx] = steps / directions.length;
}
return connections[current][dir];
});
steps++;
if (steps % 1000000 == 0) {
yield cycles;
}
}
}
yield cycles;
}