# Challenge

Given a string input, output the demolished version of it.

# The Process

P
r      Pr       r
o       o       o
g       g       g
r       r       r      rogr         r
a  ->   a  ->   a  ->     a  ->     a  ->           ->           ->           ->           ->
m       m       m         m         m
m       m       m         m         m         mmar         m
i       i       i         i         i         i            i           mi           m
n       n       n         n        gn        gn           gn           gn           gni         mgni
g       g      Pg        Pg      roPg      roPg         roPgmar      roPgmar      roPgmar      roPgmar

1. Place the string vertically.
2. Select a random integer between 1 and (height of the column of characters) - 1 and a random direction (left or right).
3. Rotate that number of characters in that direction (if those spaces are unoccupied go to step 4; if not, go back to step 2).
4. Let those characters fall due to gravity.
5. Repeat until the height of the column of characters is at most 1 larger than the height of the columns next to it (i.e. it becomes impossible to further demolish ("steps 2-4") the column).
6. If there is another column of characters that is more than 1 character taller than one or more of its surrounding columns (i.e. demolish-able), repeatedly demolish that column until it is no longer demolish-able. If there are multiple demolish-able columns, completely demolish the tallest column (if there are multiple tallest columns, completely demolish the leftmost one).
7. Repeat until all columns are no longer demolish-able.

If there are space characters in the input, demolish those first, all at once.

C
o

d
e  ->     oC  ->         ->  ...
de
G        G          G
o        o          o
l        l          l
f        f        defoC


## Rules

• Standard loopholes are forbidden.
• Trailing and leading newlines are allowed.
• Your program may either print or return a string/equivalent.
• The output must be non-deterministic (unless the input is not demolish-able).

This is , so the submissions with the smallest byte counts in their languages win!

• I would doubt if random is really necessary here – Keyu Gan Oct 13 '17 at 1:51
• @KeyuGan I think the challenge would be fairly trivial if people had to select a fixed number of characters and alternated left/right. – JungHwan Min Oct 13 '17 at 2:22
• We still can say that 4 is random and returned by a fair dice roll – the default. Oct 13 '17 at 4:02
• @someone 4 would make the output deterministic, i.e. not "random." Edited the rules to make that explicit. – JungHwan Min Oct 13 '17 at 5:00
• @someone are you referring at 4 because of XKCD? – Giacomo Garabello Oct 13 '17 at 10:03

# Python 2, 622595573552542534527520 515 bytes

from random import*
s=input()
r=range
R=choice
Z=len
L=h=Z(s)
a=[[]for _ in'  '*-~h]
S=s[::-1].split()
X=-1,1
for w in S[1:]:
for i in r(Z(w)):a[h-~i*R(X)]+=w[i]
a[h]+=S[0]
while L:
H=[map(Z,a[c-1:c+2])+[c]for c in r(1,h-~h)];D=L=[(min(n,m)-C,i)for n,C,m,i in H if~-C>min(n,m)]
while D:
_,c=min(L);n,C,m=map(Z,a[c-1:c+2]);D=X[n+2>C:1+(C-1>m)]
if D:
d=R(D);l=R(r(1,C-[0,m,n][d]));w,a[c]=a[c][-l:],a[c][:-l]
for i in r(l):a[c-~i*d]+=w[i]
for l in zip(*[l+[' ']*max(H)[1]for l in a if l])[::-1]:printl[2::5]


Try it online!

• 527 bytes – Erik the Outgolfer Oct 13 '17 at 10:15
• @EriktheOutgolfer Thanks :) – TFeld Oct 13 '17 at 10:27
• 520 bytes – ovs Oct 20 '17 at 7:33
• h+R(X)*-~i can be h-~i*R(X). – Jonathan Frech Oct 21 '17 at 3:01
• L=[...];D=L can be D=L=[...]. – Jonathan Frech Oct 21 '17 at 3:08