Algorithms

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.

Select cells to edit the grid, then press Play to start the flow.

Water inlet

Water inlet

Side walls · outlet at the bottom

The grid is dryOpen 23 / 49 · Reached by water 0

BlockedOpen and dryWater trailStanding waterTop-to-bottom path

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.

LayoutOpen cellsReached by waterReaches bottom
Starting grid2313No
Open the side cell2414No
Open the connecting cell2413Yes

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

Further reading