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

The Process

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.


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


  • 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!

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

Python 2, 622 595 573 552 542 534 527 520 515 bytes

from random import*
a=[[]for _ in'  '*-~h]
for w in S[1:]:
 for i in r(Z(w)):a[h-~i*R(X)]+=w[i]
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:
	if D:
	 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]:print`l`[2::5]

Try it online!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.