Skip to main content
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

Given the 0-indexed position \$n\$ of the character, the direction \$0\le d\le 2\$ is given by:

$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$

Try it online! (with extra whitespace removed)

How?

Given the position \$n\$ of the character, the direction \$0\le d\le 2\$ can be computed with:

$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$

The actual JS implementation is:

~(2 * n ** 0.5 - 1 / n) % 4 % 3

which evaluates to \$0\$, \$-1\$ or \$-2\$.

Given the 0-indexed position \$n\$ of the character, the direction \$0\le d\le 2\$ is given by:

$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$

Try it online! (with extra whitespace removed)

Try it online! (with extra whitespace removed)

How?

Given the position \$n\$ of the character, the direction \$0\le d\le 2\$ can be computed with:

$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$

The actual JS implementation is:

~(2 * n ** 0.5 - 1 / n) % 4 % 3

which evaluates to \$0\$, \$-1\$ or \$-2\$.

added a shorter version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (ES8), 137 bytes

Expects an array of characters. Returns a string.

This version is based on the formula used by @att, modified to be more golf-friendly in JS.

Given the 0-indexed position \$n\$ of the character, the direction \$0\le d\le 2\$ is given by:

$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$

a=>a.map((c,n)=>(m[y+=~(d=2*n**.5-1/n)%4%3?d&++x/x||-1:!x--]=m[y]||[...''.padEnd(x)])[x]=c,m=[],x=y=a.length)&&m.map(r=>r.join``).join`
`

Try it online! (raw output)

Try it online! (with extra whitespace removed)


JavaScript (ES8),  163  157 bytes

JavaScript (ES8),  163  157 bytes

JavaScript (ES8), 137 bytes

Expects an array of characters. Returns a string.

This version is based on the formula used by @att, modified to be more golf-friendly in JS.

Given the 0-indexed position \$n\$ of the character, the direction \$0\le d\le 2\$ is given by:

$$d=\left(\left\lfloor2\sqrt{n}+1-\frac{1}{n}\right\rfloor\bmod 4\right)\bmod 3$$

a=>a.map((c,n)=>(m[y+=~(d=2*n**.5-1/n)%4%3?d&++x/x||-1:!x--]=m[y]||[...''.padEnd(x)])[x]=c,m=[],x=y=a.length)&&m.map(r=>r.join``).join`
`

Try it online! (raw output)

Try it online! (with extra whitespace removed)


JavaScript (ES8),  163  157 bytes

added more details about the update of the coordinates
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

And the corresponding expression to update \$k\$:

k = (n = j / 3 << 1) + (j++ % 3 || n + 2)

The coordinates are updated with:

j % 3 % 2 ?          // if the direction is 1:
  x--                //   decrement x
:                    // else:
  y += !!++x - j % 3 //   increment y if the direction is 0
                     //   or decrement y if it's 2
                     //   increment x in both cases

And the expression:

k = (n = j / 3 << 1) + (j++ % 3 || n + 2)

And the corresponding expression to update \$k\$:

k = (n = j / 3 << 1) + (j++ % 3 || n + 2)

The coordinates are updated with:

j % 3 % 2 ?          // if the direction is 1:
  x--                //   decrement x
:                    // else:
  y += !!++x - j % 3 //   increment y if the direction is 0
                     //   or decrement y if it's 2
                     //   increment x in both cases
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
added more details about the way k is computed
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
added more details about the directions and distances
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
saved 6 bytes
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading