Skip to main content
1 of 4
lebatsnok
  • 403
  • 4
  • 7

R, 196 bytes

function(x){l=nchar;o=function(y)head(l(y),-1)>l(y)[-1];d=function(x,i){j=i+1;xi=x[i];xj=x[j];"[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj)))};while(any(o(x)))x=d(x,which(o(x))[1]);x} 

A readable version:

f<-function(x){ l=nchar; o=function(y)head(l(y),-1)>l(y)[-1]; d=function(x,i){ j=i+1;xi=x[i];xj=x[j]; "[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj))) } while(any(o(x)))x=d(x,which(o(x))[1]);x } 

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 
lebatsnok
  • 403
  • 4
  • 7