Wrap a seasonal present
J, 16 15 bytes
[h"2[h"1 h=.,,[
This is an anonymous verb. Try it online!
Thanks to Adám for 1 byte!
Explanation
[h"2[h"1 h=.,,[ Wrapper is x, present is y.
, Prepend x to y
, then append
[ x.
This gives x y x, and the wrapper automatically spreads to form 2D slices.
h=. Save the above operation (not its result) to h.
[h"1 Apply h to x and every 2D slice of the previous result.
[h"2 Apply h to x and every 1D slice of the result of that.
JavaScript (ES6), 97 bytes
(a,e)=>[c=[,,...b=a[0]].fill(d=[,,...b[0]].fill(e)),...a.map(a=>[d,...a.map(a=>[e,...a,e]),d]),c]
Where a
is the three-dimensional array and e
is the wrapper. Automatically converts a two-dimensional array of strings to a three-dimensional array of characters. Alternative version for when a
is a two-dimensional array of strings and e
is a character and you want to return a two-dimensional array of strings:
(a,e)=>[c=[,,...a[0]].fill(d=e.repeat(a[0][0].length+2)),...a.map(b=>[c,...b.map(s=>e+s+e),d]),c]
Dyalog APL, 31 19 13 12 bytes
Almost a transliteration (31 bytes) of @Zgarb's solution.
An anonymous function. Left argument is wrapping, right argument is gift.
⊣h⍤1⊣h⍤2(h←⍪⍪⊣)
⊣h⍤1
h applied, with the anonymous function's left argument, to the columns of
⊣h⍤2
h applied, with the anonymous function's left argument, to the rows of
h←
h applied to the major cells i.e. the layers of the anonymous function's arguments, where h is
⍪
the left argument prepended to the right argument
⍪
prepended to
⊣
the left argument
In other words, h is a function which surrounds its right argument (the gift) with its left argument (the wrapper). h is then applied to the gift's layers, then the rows of that, and finally the columns of that.
TryAPL online!
This Dyalog APL version 16.0 solution (19 bytes – courtesy of @ngn) handles any number of dimensions:
{⍵@(1+⍳⍴⍵)⊢⍺⍴⍨2+⍴⍵}
⍵
the gift
@(
placed at
1+
one plus
⍳
all the indices of
⍴⍵
the shape of the gift
)⊢
in the array consisting of
⍺⍴⍨
the wrapper reshaped to the shape
2+
two added to
⍴⍵
the shape of the gift
In other words, we create an array entirely of wrapper elements, which in every dimension is two elements larger than the gift, then we place the gift into that array (thus replacing the wrapping elements in those positions) at an offset of one from the edges, i.e. in the center.
My own invention (-1 thanks to @ngn):
(⌽2 3 1⍉,)⍣6
This applies an anonymous function-train 6 times, each time with the wrapper as left argument, and the result of the previous application as right argument (although the first time around it will be the unmodified gift):
(
an anonymous function-train
⌽
reverse columns of
2 3 1⍉
the rows-to-layers, columns-to-rows, layers-to-columns transposition of
,
the wrapper followed by the gift
)⍣6
applied six times
In other words, we add a layer of wrapper on the top of the array, then warp it so that the next side gets rotated into the top layer position, ready for another round of wrapping. This is repeated six times, with the final warping repositioning all axes to the original order.
TryAPL online!