Skip to main content
Explain
Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Out dated Here I explain the first solution, both are the same length but I like the first one because it is cooler and employs some nice tricks.

This is based offThe most important part of the code is a neat littlemodified square root snippetfunction I wrote some time ago. The original version was

It onlyAnd this works on perfect squares, but that's okwe actually want two copies of the negative square root. Why? We need two copies because we are looping through the input will alwaysstring at two levels, one to make the lines and one to count the number of lines. We want it to be a perfect squarenegative because looping with negatives is cheaper.

Here is an annotated version ofTo make this negative we move around the code[...] so it looks like this

{([{}([]{})(<{}[()>]))<             #}{Get the stack height and put zero under it for the square root rutine}
  

To make two copies we change when pops occur

{({}[({})(({}[())])))}{}    #{Perform the square root}
>]

Now that we have that bit we can put it together with a stack height to get the first chunk of code we need.

([]<>)                       #{Recall the stack height({}
 {}(({}[()])))}{}

We move to the offstack because our square root function needs two free zeros for computation, and because it makes stuff a little bit cheaper int he future in terms of stack switching.

Now we construct the main loop

{({}()<                #(({n times}
  )<{({}[()]<<<>({}<>)<>>>)}{}  #{move n items to the offstack}
  <>((()()()()()){})<>    #{Put a newline on the other stack}
>)>)}                      #{End loop}
 {}{}                      #{Cleanup}
<>

This is pretty straight forward, we loop n times each time moving n items and capping it with a new line (ASCII 10).

Once the loop is done we need to reverse the order of our output so we just tack on a standard reverse construct.

{({}<>)<>}<>            #{Move everything back}

Out dated

This is based off of a neat little square root snippet I wrote some time ago.

It only works on perfect squares but that's ok because the input will always be a perfect square.

Here is an annotated version of the code

([([](<()>))<             #{Get the stack height and put zero under it for the square root rutine}
  {({}[({})({}())])}{}    #{Perform the square root}
>])                       #{Recall the stack height}
 {({}(({})<                #{n times}
  {({}[()]<({}<>)<>>)}{}  #{move n items to the offstack}
  <>((()()()()()){})<>    #{Put a newline on the other stack}
>))}                      #{End loop}
 {}{}                      #{Cleanup}
<>{({}<>)<>}<>            #{Move everything back}

Here I explain the first solution, both are the same length but I like the first one because it is cooler and employs some nice tricks.

The most important part of the code is a modified square root function I wrote some time ago. The original version was

And this works, but we actually want two copies of the negative square root. Why? We need two copies because we are looping through the string at two levels, one to make the lines and one to count the number of lines. We want it to be negative because looping with negatives is cheaper.

To make this negative we move around the [...] so it looks like this

{({}({})({}[()]))}{}

To make two copies we change when pops occur

{({}{}(({}[()])))}{}

Now that we have that bit we can put it together with a stack height to get the first chunk of code we need.

([]<>){({}{}(({}[()])))}{}

We move to the offstack because our square root function needs two free zeros for computation, and because it makes stuff a little bit cheaper int he future in terms of stack switching.

Now we construct the main loop

{({}()<(({})<{({}()<<>({}<>)>)}{}((()()()()()){})>)>)}{}{}

This is pretty straight forward, we loop n times each time moving n items and capping it with a new line (ASCII 10).

Once the loop is done we need to reverse the order of our output so we just tack on a standard reverse construct.

{({}<>)<>}<>
Golf
Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Brain-Flak, 110110 96 bytes

([]<>){({}{}(({}[([])])))}{}{({}()<(({})<{({}()<<>({}<>)>)}{}((()()()()()){})>)>)}{}{}{({}<>)<>}<>

Try it online!

Second solution, 96 bytes

(([]<>)<{({}[({})({}[())]))}{}>]>){({}(({})<{({}[()]<<<>({}<>)<>>>)}{}<>((()()()()()){})<>>>))}{}{}<>{({}<>)<>}<>

Try it online!Try it online!

Explanation

Out dated

This is based off of a neat little square root snippet I wrote some time ago.

{({}[({})({}())])}{}

It only works on perfect squares but that's ok because the input will always be a perfect square.

Here is an annotated version of the code

([([](<()>))<             #{Get the stack height and put zero under it for the square root rutine}
  {({}[({})({}())])}{}    #{Perform the square root}
>])                       #{Recall the stack height}
{({}(({})<                #{n times}
  {({}[()]<({}<>)<>>)}{}  #{move n items to the offstack}
  <>((()()()()()){})<>    #{Put a newline on the other stack}
>))}                      #{End loop}
{}{}                      #{Cleanup}
<>{({}<>)<>}<>            #{Move everything back}

Brain-Flak, 110 bytes

([([](<()>))<{({}[({})({}())])}{}>]){({}(({})<{({}[()]<({}<>)<>>)}{}<>((()()()()()){})<>>))}{}{}<>{({}<>)<>}<>

Try it online!

Explanation

This is based off of a neat little square root snippet I wrote some time ago.

{({}[({})({}())])}{}

It only works on perfect squares but that's ok because the input will always be a perfect square.

Here is an annotated version of the code

([([](<()>))<             #{Get the stack height and put zero under it for the square root rutine}
  {({}[({})({}())])}{}    #{Perform the square root}
>])                       #{Recall the stack height}
{({}(({})<                #{n times}
  {({}[()]<({}<>)<>>)}{}  #{move n items to the offstack}
  <>((()()()()()){})<>    #{Put a newline on the other stack}
>))}                      #{End loop}
{}{}                      #{Cleanup}
<>{({}<>)<>}<>            #{Move everything back}

Brain-Flak, 110 96 bytes

([]<>){({}{}(({}[()])))}{}{({}()<(({})<{({}()<<>({}<>)>)}{}((()()()()()){})>)>)}{}{}{({}<>)<>}<>

Try it online!

Second solution, 96 bytes

(([]<>)<{({}({})({}[()]))}{}>){({}(({})<{({}()<<>({}<>)>)}{}((()()()()()){})>))}{}{}{({}<>)<>}<>

Try it online!

Explanation

Out dated

This is based off of a neat little square root snippet I wrote some time ago.

{({}[({})({}())])}{}

It only works on perfect squares but that's ok because the input will always be a perfect square.

Here is an annotated version of the code

([([](<()>))<             #{Get the stack height and put zero under it for the square root rutine}
  {({}[({})({}())])}{}    #{Perform the square root}
>])                       #{Recall the stack height}
{({}(({})<                #{n times}
  {({}[()]<({}<>)<>>)}{}  #{move n items to the offstack}
  <>((()()()()()){})<>    #{Put a newline on the other stack}
>))}                      #{End loop}
{}{}                      #{Cleanup}
<>{({}<>)<>}<>            #{Move everything back}
Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Brain-Flak, 110 bytes

([([](<()>))<{({}[({})({}())])}{}>]){({}(({})<{({}[()]<({}<>)<>>)}{}<>((()()()()()){})<>>))}{}{}<>{({}<>)<>}<>

Try it online!

Explanation

This is based off of a neat little square root snippet I wrote some time ago.

{({}[({})({}())])}{}

It only works on perfect squares but that's ok because the input will always be a perfect square.

Here is an annotated version of the code

([([](<()>))<             #{Get the stack height and put zero under it for the square root rutine}
  {({}[({})({}())])}{}    #{Perform the square root}
>])                       #{Recall the stack height}
{({}(({})<                #{n times}
  {({}[()]<({}<>)<>>)}{}  #{move n items to the offstack}
  <>((()()()()()){})<>    #{Put a newline on the other stack}
>))}                      #{End loop}
{}{}                      #{Cleanup}
<>{({}<>)<>}<>            #{Move everything back}