# Lay out the Carpet

## R, 11895 92 bytes

function(a,d,n=length(a),I=c(n:1,1:n)[-n])for(i in I-1)write(c(a,d)[pmin(I+i,n+1)],1,n*2,,d)


Try it online!

Thanks to:

• Giuseppe for fixing an error and a golf
• Aaron Hayman for 22 bytes worth of golf

## J, 59 56 bytes

,{~[:((0-2*#)}.\[:,0,:"0({:>:t)*t=:]+/<:)[:(|[email protected]}.,])#\@]


Try it online!

Too long solution for J ... (entirely my fault)

## R, an ugly 118 bytes version

By letting the input be a vector of single characters, and outputting a matrix instead of printing nice ascii art.

function(s,C,l=length(s),L=4*l-3,k=2*l-1,y=abs(rep(1:k,L)-l)+abs(rep(1:L,e=k)-k)/2+1)matrix(ifelse(y%%1|y>l,C,s[y]),k)


Try it online!

## R, 161 157 bytes

saved 4 bytes by using ifelse instead of conditionally modifying y

function(S,C,l=nchar(S),L=4*l-3,k=2*l-1,y=abs(rep(1:L,k)-k)/2+abs(rep(1:k,e=L)-l)+1)cat(rbind(matrix(ifelse(y%%1|y>l,C,el(strsplit(S,''))[y]),L),'
'),sep='')


Try it online!

### ungolfed and commented

function(S,C){
s=el(strsplit(S,''))
l=nchar(S)
L=4*l-3
k=2*l-1
y=abs(rep(1:L,k)-k)/2+abs(rep(1:k,e=L)-l)+1 # distance from centre
y[!!y%%1]=l+1  # set non integers to one more than length of string
y[y>l]=l+1     # set number beyond length of string to one more than length of string
M = rbind(matrix(c(s,C)[y],L),'\n') # build matrix and add line returns
cat(M,sep='') # print the matrix as a string
}


hmmm, seems like the longest answer so far!