function* part2Generator(input, skip = 100) {
let grid = makeGrid(input);
yield grid.clone();
let lastSandLevel = d3.max(grid, (d) => d[0].y) + 1;
let sandCount = 0;
let budget = 1e7;
while (budget-- > 0) {
const sand = { ...sandFrom };
while (budget-- > 0) {
let stopped = false;
if (!grid.has({ ...sand, y: sand.y + 1 })) sand.y += 1;
else if (!grid.has({ x: sand.x - 1, y: sand.y + 1 })) {
sand.x -= 1;
sand.y += 1;
} else if (!grid.has({ x: sand.x + 1, y: sand.y + 1 })) {
sand.x += 1;
sand.y += 1;
} else {
stopped = true;
}
if (sand.y >= lastSandLevel) {
stopped = true;
}
if (stopped) {
grid.set(sand, "sand");
if (sand.x === sandFrom.x && sand.y === sandFrom.y) {
yield grid.clone();
return;
}
sandCount += 1;
if (sandCount % skip === 0) yield grid.clone();
break;
}
}
}
console.log("budget exceeded");
}