straight line floodfill SIMPLIFIED by jg10

l=list;ds=l(c(0,1),c(0,-1),c(1,0),c(-1,0));m=matrix(unlist(strsplit(x<-readLines("stdin"),"")),nr=NROW(x),b=T);ss=l(which(m==".",a=T));sd=oldm=NULL;while(!identical(oldm,m)){oldm=m;for(s in setdiff(ss,sd)){sd=c(sd,l(s));for(d in ds){p=s+d;repeat{x=m[p[1],p[2]];if(x==" "){m[p[1],p[2]]="."}else if(x=="#"){ss=c(ss,l(p-d));break};p=p+d}}}};write(t(m),"",sep="",nc=ncol(m))

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page