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 2915 characters in body
Source Link
recursive
  • 10.5k
  • 21
  • 36

Unpacked, ungolfed, and commented it looks like this.

 input e.g. "big socks"
L$j$ remove whitespace from input e.g. "bigsocks"
cr{~F copy push each remaining character to input stack in reverse order
%c get length L of flattened input and copy e.g. 8
^r3:$ create triples [a,b,c] in range 0..n e.g. [[0,0,0], [0,0,1], ... [8,8,8]]
{E*NsJ+n=f filter triples: require a*a-b*c=L e.g. [[3,1,1], [4,1,8], ...]
{ET>f filter triples: a>b and a>c e.g. [[3,1,1]]
{{2%m:u_0#+f filter triples: a%2=b%2=c%2 or b*c=0 e.g. [[3,1,1]]
":~("s|c if there are no triples, output ":~(" and terminate
{D:s_D:*\oh sort by [abs(b-c),b*c]; keep the first e.g. [3,1,1]
X store the chosen triple in the X register
E explode onto stack e.g. 3, 1, 1
z)]* using last two values, make a rectangle of zeroes e.g. 3, [[0]]
~ this will be the cutout, push it to the input stack
c'X*]* make a square of "X" e.g. ["XXX", "XXX", "XXX"]
xEd-h given the dimensions in x register, calculate the centered cutout coordinates
xEsd-h e.g. ["XXX", "XXX", "XXX"], 1, 1
,|| embed the cutout grid at the specified coordinates e.g. ["XXX", "X X", "XXX"]
m'X{,]}R for each line, regex replace "X" with a character from the input stack

Run this one

Unpacked, ungolfed, and commented it looks like this.

 input e.g. "big socks"
L$j$ remove whitespace from input e.g. "bigsocks"
cr{~F copy push each remaining character to input stack in reverse order
%c get length L of flattened input and copy e.g. 8
^r3:$ create triples [a,b,c] in range 0..n e.g. [[0,0,0], [0,0,1], ... [8,8,8]]
{E*NsJ+n=f filter triples: require a*a-b*c=L e.g. [[3,1,1], [4,1,8], ...]
{ET>f filter triples: a>b and a>c e.g. [[3,1,1]]
{{2%m:u_0#+f filter triples: a%2=b%2=c%2 or b*c=0 e.g. [[3,1,1]]
":~("s|c if there are no triples, output ":~(" and terminate
{D:s_D:*\oh sort by [abs(b-c),b*c]; keep the first e.g. [3,1,1]
X store the chosen triple in the X register
E explode onto stack e.g. 3, 1, 1
z)]* using last two values, make a rectangle of zeroes e.g. 3, [[0]]
~ this will be the cutout, push it to the input stack
c'X*]* make a square of "X" e.g. ["XXX", "XXX", "XXX"]
xEd-h given the dimensions in x register, calculate the centered cutout coordinates
xEsd-h e.g. ["XXX", "XXX", "XXX"], 1, 1
,|| embed the cutout grid at the specified coordinates e.g. ["XXX", "X X", "XXX"]
m'X{,]}R for each line, regex replace "X" with a character from the input stack

Run this one

Source Link
recursive
  • 10.5k
  • 21
  • 36

Stax, 80 bytes

ö╦Çc▀╕╡ëé╓]μiÖ9♪`W|╣▐↑╖D┘↕♫╗╔äƒしかくúφしかくU`ÿ⌠%é┴☼vAú)☺@ı◄¬¶¢(îÉ┼6⌠D~♀└lfæA.I@o╤∟òîü╦(

Run and debug it

How does it work?

  • Consider all squares with rectangle cutouts.
  • Filter out program shapes that aren't the right size.
  • Filter out program shapes that don't have matching parity.
  • Filter out program shapes where the cutout is too big for the square.
  • If none are found, output failure and quit.
  • Find the shape that minimizes "rectangularity" of cutout, then by cutout size.
  • Replace each character in the program shape with the corresponding character in the program input.

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