Demolish a string!

12

2

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!

JungHwan Min

Posted 2017-10-12T23:56:51.800

Reputation: 13 290

1I would doubt if random is really necessary here – Keyu Gan – 2017-10-13T01:51:12.023

@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 – 2017-10-13T02:22:12.437

4We still can say that 4 is random and returned by a fair dice roll – my pronoun is monicareinstate – 2017-10-13T04:02:39.177

@someone 4 would make the output deterministic, i.e. not "random." Edited the rules to make that explicit. – JungHwan Min – 2017-10-13T05:00:17.500

@someone are you referring at 4 because of XKCD?

– Giacomo Garabello – 2017-10-13T10:03:51.890

Yes, but that reference is not the main idea of the comment. – my pronoun is monicareinstate – 2017-10-13T10:18:07.500

Answers

5

Python 2, 622 595 573 552 542 534 527 520 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]:print`l`[2::5]

Try it online!

TFeld

Posted 2017-10-12T23:56:51.800

Reputation: 19 246

527 bytes – Erik the Outgolfer – 2017-10-13T10:15:13.280

@EriktheOutgolfer Thanks :) – TFeld – 2017-10-13T10:27:54.800

520 bytes – ovs – 2017-10-20T07:33:36.740

h+R(X)*-~i can be h-~i*R(X). – Jonathan Frech – 2017-10-21T03:01:13.307

L=[...];D=L can be D=L=[...]. – Jonathan Frech – 2017-10-21T03:08:18.657

' '*-~h*2 can be -~h*' ' (the string contains two spaces). – Jonathan Frech – 2017-10-21T03:23:03.237

515 bytes. – Jonathan Frech – 2017-10-21T03:28:33.113