Suppose this grid of spaces and X
's represents the cross section of some strangely shaped empty ice cube trays:
X X X
X X X XX X XX X
XXXXXX XXXXXXXXXXXXX
Columns without X
's represent holes or gaps in the trays that can't hold water, draining into an infinite capacity sink. Water falling off the leftmost or rightmost edge of the grid goes into this endless sink as well.
If we were to position a faucet above the trays and let them fill with water until the water level in all compartments remains stable, the exact compartments that become filled would depend on exactly where the water stream was positioned above the trays. (Assume a thin, steady stream of water with no splashing.)
For example, if our faucet F
were above the very left grid column
F
X X X
X X X XX X XX X
XXXXXX XXXXXXXXXXXXX
the water would fall down to the topmost X
in that column and spread left and right, the left half spilling into the sink below, and the right half filling up the 2×1 compartment. Once the compartment fills, the right half of the water stream has nowhere to flow but into the sink and the water level everywhere is essentially stable.
Turning the faucet off, the tray now looks like this: (with ~
as water)
X X X
X~~X X XX X XX X
XXXXXX XXXXXXXXXXXXX
Similarly, if we position the faucet like this:
F
X X X
X X X XX X XX X
XXXXXX XXXXXXXXXXXXX
It will fill up the two leftmost compartments but the rest of the water will drain away:
X X X
X~~X~X XX X XX X
XXXXXX XXXXXXXXXXXXX
If we position the faucet like this:
F
X X X
X X X XX X XX X
XXXXXX XXXXXXXXXXXXX
The left half of the stream will flow into the sink but the right half will eventually fill up the three rightmost compartments because there's no limit to how far water can travel horizontally on a flat surface:
X X~X
X X X XX~X~~XX~~~X
XXXXXX XXXXXXXXXXXXX
Positioned like this, however:
F
X X X
X X X XX X XX X
XXXXXX XXXXXXXXXXXXX
All the water drains away and no compartments are filled:
X X X
X X X XX X XX X
XXXXXX XXXXXXXXXXXXX
Challenge
Write a program or function that takes in a rectangular grid of spaces, X
's, and one F
. The top row will always contain the F
and otherwise only contain spaces. The X
's in each column (if there are any) will extend in a solid line up from the base of the grid, i.e. there will be no caves or overhangs.
Print or return the grid after the faucet F
has filled what it can with water ~
as described above. Leave the top F
row out of the output.
The grid apart from the faucet row will be 1×1 at minimum so
F X
is the smallest input you need to support.
The input will come in as a complete text rectangle. Leading and trailing spaces do matter in the input and output. e.g. the input
F X X XXXX
should result in
X~~X XXXX
(note the leading and trailing spaces)
Having a single trailing newline in the input or output is fine.
You can use any four distinct printable ASCII characters in place of space,
X
,F
,~
.
The shortest code in bytes wins.
Big Example:
Input:
F
X X
X X X
X XXX X X X X X
X X XXXXXXX X XXX XXXXXXX X X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXX
Output:
X~~~~~~~~~~~~~X
X~~~~~~~~~~~~~X~X
X~~~~~~~~~~~~XXX~~~~~~~X~~~~X~X~~~~~~~~~~~X X
X~~~X~~~~~XXXXXXX~~~~~~X~~~~XXX~~~~~XXXXXXX X X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXX
zip()
<3 \$\endgroup\$