Challenge
Given a rectangular grid of printable ascii characters as a string or through standard input, write a function or program that collapses the non-space characters into a pile on the bottom.
The rules:
- The output has the same dimensions and characters as the input.
- A non-space character at
(row a,column b)
can't have a space character' '
at(a-1, b)
,(a-1,b-1)
or(a-1,b+1)
, where rows are numbered up from the bottom-most. This has the consequence that all vertical piles should collapse sideways. - A non-space character can travel at most
(initial height - final height)
places to the left or right (see Fig. 1). - You can assume the picture has enough space to collapse without characters falling out of the screen.
Figure 1: possible final locations for characters @#$
shown as x,y,z
, respectively.
..............
...@..........
..xxx.........
.xxxxx...#....
xxxxxxx.yyy.$.
The order in which characters collapse can be chosen freely. Trailing spaces are not okay but trailing newlines are.
This is code-golf, so the shortest answer in bytes wins!
Example
(__)
(oo)
/------\/
/ | ||
* /\---/\
~~ ~~
..."Have you mooed today?"...
One possible output:
--(_
/----|/|(o_)
/|/~\---~\\/o)
..."Have*you~mooed~today?"...