Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added a commented version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
f=(a,x=W=a.every(c=>w+=W=cn=>w+=N=n,w=y=0)+w*W+w*N)=>(q=W-a[y/w|0]*w)?` *
`[--x?-~y%w<2|(X=qX=x+q/2+x2)%w<2&&X>=q&X<W%w<2|-~y%w<2&&X>=q&X<W:++y&&2]+f(a,x||W):''

Try it online! Try it online!

Commented

f = ( // f is a recursive function taking:
 a, // a[] = input array
 // and also using:
 x = // (x, y) = current position
 W = // W = total row width, w = crate width
 a.every(n => // for each value n in a[]:
 w += N = n, // add n to w and save n in the global scope
 w = y = 0 // start with w = 0 and y = 0
 ) // end of every(), which returns 1
 + w * N // add the last value multiplied by w
) => //
( q = W - // define q as the total row width less the
 a[y / w | 0] * w // total width of the crates on this row
) ? // if q is not NaN:
 ` *\n`[ // character lookup:
 --x ? // decrement x; if it's not 0:
 (X = x + q / 2) // define X as x + q / 2
 % w < 2 | // if we're over a vertical crate edge
 -~y % w < 2 // or over a horizontal crate edge
 && // and
 X >= q & X < W // we're over the crates in this row,
 // then put a '*', else put a space
 : // else:
 ++y && 2 // increment y and put a linefeed
 ] + // end of character lookup
 f(a, // append the result of a recursive call
 x || W) // make sure to restart with x = W if x = 0
: // else:
 '' // stop the recursion
f=(a,x=W=a.every(c=>w+=W=c,w=y=0)+w*W)=>(q=W-a[y/w|0]*w)?` *
`[--x?-~y%w<2|(X=q/2+x)%w<2&&X>=q&X<W:++y&&2]+f(a,x||W):''

Try it online!

f=(a,x=W=a.every(n=>w+=N=n,w=y=0)+w*N)=>(q=W-a[y/w|0]*w)?` *
`[--x?(X=x+q/2)%w<2|-~y%w<2&&X>=q&X<W:++y&&2]+f(a,x||W):''

Try it online!

Commented

f = ( // f is a recursive function taking:
 a, // a[] = input array
 // and also using:
 x = // (x, y) = current position
 W = // W = total row width, w = crate width
 a.every(n => // for each value n in a[]:
 w += N = n, // add n to w and save n in the global scope
 w = y = 0 // start with w = 0 and y = 0
 ) // end of every(), which returns 1
 + w * N // add the last value multiplied by w
) => //
( q = W - // define q as the total row width less the
 a[y / w | 0] * w // total width of the crates on this row
) ? // if q is not NaN:
 ` *\n`[ // character lookup:
 --x ? // decrement x; if it's not 0:
 (X = x + q / 2) // define X as x + q / 2
 % w < 2 | // if we're over a vertical crate edge
 -~y % w < 2 // or over a horizontal crate edge
 && // and
 X >= q & X < W // we're over the crates in this row,
 // then put a '*', else put a space
 : // else:
 ++y && 2 // increment y and put a linefeed
 ] + // end of character lookup
 f(a, // append the result of a recursive call
 x || W) // make sure to restart with x = W if x = 0
: // else:
 '' // stop the recursion
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), (削除) 132 128 120 (削除ここまで) 120119 bytes

Non-centered crates are shifted to the right.

f=(a,x=W=a.mapevery(c=>w+=W=c,w=y=0)&&w*W+1+w*W)=>(q=W-a[y/w|0]*w)?`*` *
`[--x?-~y%w>1&~y%w<2|(X=q/2+x)%w>=2|X<q|X>=W%w<2&&X>=q&X<W:++y&&2]+f(a,x||W):''

Try it online! Try it online!

JavaScript (ES6), (削除) 132 128 (削除ここまで) 120 bytes

Non-centered crates are shifted to the right.

f=(a,x=W=a.map(c=>w+=W=c,w=y=0)&&w*W+1)=>(q=W-a[y/w|0]*w)?`* 
`[--x?-~y%w>1&(X=q/2+x)%w>=2|X<q|X>=W:++y&&2]+f(a,x||W):''

Try it online!

JavaScript (ES6), (削除) 132 128 120 (削除ここまで) 119 bytes

Non-centered crates are shifted to the right.

f=(a,x=W=a.every(c=>w+=W=c,w=y=0)+w*W)=>(q=W-a[y/w|0]*w)?` *
`[--x?-~y%w<2|(X=q/2+x)%w<2&&X>=q&X<W:++y&&2]+f(a,x||W):''

Try it online!

saved 8 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 128(削除) 132 128 (削除ここまで) 120 bytes

Non-centered crates are shifted to the right.

a=>(w=evalf=(a,x=W=a.join`+`)map(c=>w+=W=c,g=x=>w=y=0)&&w*W+1)=>(q=W-a[y/w|0]*w)?`* 
`[--x?-~y%w>1&(X=q/2+x)%w>=2|X<q|X>=W:++y&&2]+g++y&&2]+f(a,x||W):'')(W=[...a].pop(y=0)*w+1)

Try it online! Try it online!

JavaScript (ES6), 128 bytes

Non-centered crates are shifted to the right.

a=>(w=eval(a.join`+`),g=x=>(q=W-a[y/w|0]*w)?`* 
`[--x?-~y%w>1&(X=q/2+x)%w>=2|X<q|X>=W:++y&&2]+g(x||W):'')(W=[...a].pop(y=0)*w+1)

Try it online!

JavaScript (ES6), (削除) 132 128 (削除ここまで) 120 bytes

Non-centered crates are shifted to the right.

f=(a,x=W=a.map(c=>w+=W=c,w=y=0)&&w*W+1)=>(q=W-a[y/w|0]*w)?`* 
`[--x?-~y%w>1&(X=q/2+x)%w>=2|X<q|X>=W:++y&&2]+f(a,x||W):''

Try it online!

saved 4 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /