Percolation — one cell lets water through
Press Play to see where the water stops on the starting grid. Open the outlined cell in row 5, column 4, then press Play again. This time the water reaches the bottom edge.
How water moves
Imagine a slice of a porous material as a 7 × 7 grid. Water comes in from the top. It drops through an open cell whenever it can. If the way down is blocked, water spreads to both open neighbours in the same step.
Solid walls sit just outside the left and right columns. Water can enter a cell next to a wall and keep moving inside the grid. The experiment ends when water first reaches an open cell on the bottom edge.
When a row is bounded by walls and supported from below, the water pools across that whole span at the same height. The lowest part of a pocket fills first. On the next step water rises into every open cell immediately above the pool.
By default, water enters through all open cells in the top row. Use the numbered buttons to feed it into one or more selected columns. Select All to restore the default and Play to start the new run.
Previous episode · Menger sponge
Same number of openings, different result
With All selected for the water inlet, run the starting grid first. The bottom stays dry. Open the connecting cell in row 5, column 4 and start the flow again. Water drops straight through the new opening and reaches the outlet. Look on either side of the stream in row 6. Both neighbouring cells are open and stay dry.
Use the Side cell preset and start the flow again. This grid also has 24 open cells. It gains one wet cell near the top and still has no outlet. Restart clears the water and preserves your grid. Original grid restores the starting layout.
| Layout | Open cells | Reached by water | Reaches bottom |
|---|---|---|---|
| Starting grid | 23 | 13 | No |
| Open the side cell | 24 | 14 | No |
| Open the connecting cell | 24 | 13 | Yes |
Flow and connectivity
In site percolation, we ask whether a chain of open cells sharing sides connects the top and bottom edges. Breadth-first search can answer that connectivity question. Here the animation follows a simple gravity rule to show which cells water reaches by the time it first gets to the bottom.
Water reaches the open cells under the selected inlets on the first step. Flow goes down if possible or to both sides if supported. Standing water can rise from any cell of a full span into the row above. The algorithm plans all moves from the current state and applies them together. The first step that reaches the bottom ends the run. Recorded predecessors reconstruct one route to the outlet.
Each step scans the wet cells and the open spans that can fill with standing water. Each cell can be reached once and become standing water once. On an R × C grid this gives O((RC)²) worst-case time and O(RC) memory for the simulation. The player also stores snapshots for seeking.
JavaScript implementation
A dot is an open cell and a hash is a wall. Optionally pass an array of zero-based inlet columns. The function returns the first route to the bottom as zero-based [row, column] pairs, or an empty array when the water stops above it.
Show executable example
function waterPath(grid, sources) {
const columns = grid[0].length;
const open = grid.join("").split("").map(cell => cell === ".");
const parent = Array(open.length).fill(-2);
const wet = [];
const pooled = new Set();
const spans = grid.flatMap((row, r) =>
Array.from(row.matchAll(/\.+/g), match =>
Array.from(match[0], (_, n) => r * columns + match.index + n)));
const supported = cell => !open[cell + columns] || pooled.has(cell + columns);
const inlets = sources && new Set(sources);
let arrivals = new Map();
let settling = [];
for (let cell = 0; cell < columns; cell++) {
if (open[cell] && (!inlets || inlets.has(cell))) arrivals.set(cell, -1);
}
while (arrivals.size || settling.length) {
for (const [cell, from] of arrivals) {
parent[cell] = from;
wet.push(cell);
}
for (const cell of settling) pooled.add(cell);
const outlet = wet.find(cell => cell >= open.length - columns);
if (outlet !== undefined) {
const path = [];
for (let cell = outlet; cell !== -1; cell = parent[cell]) {
path.push([Math.floor(cell / columns), cell % columns]);
}
return path.reverse();
}
arrivals = new Map();
function propose(cell, from) {
if (open[cell] && parent[cell] === -2 && !arrivals.has(cell)) arrivals.set(cell, from);
}
for (const cell of wet) {
if (pooled.has(cell)) {
if (cell >= columns) propose(cell - columns, cell);
} else if (!supported(cell)) {
propose(cell + columns, cell);
} else {
const column = cell % columns;
if (column > 0) propose(cell - 1, cell);
if (column + 1 < columns) propose(cell + 1, cell);
}
}
settling = spans.filter(span =>
!pooled.has(span[0]) && span.every(cell =>
supported(cell) && (parent[cell] !== -2 || (arrivals.has(cell) && pooled.has(cell + columns))))
).flat();
}
return [];
}
const demo = [
"#..##.#",
"##..###",
"#...#.#",
"....##.",
"#.###.#",
"##....#",
"#.#..##"
];
const opened = demo.slice();
opened[4] = demo[4].slice(0, 3) + "." + demo[4].slice(4);
console.log(waterPath(demo)); // []
console.log(waterPath(opened)); // route from the top to the bottom