Challenge
Given a string of any length, write it as a triangle, spiraling out from the center. For example, abcdefghijklmnop
becomes:
g
fah
edcbi
ponmlkj
Or more explicitly:
If you like, you can spiral counter-clockwise instead:
g
haf
ibcde
jklmnop
Or add spaces uniformly:
g
f a h
e d c b i
p o n m l k j
The input characters will be ascii, but may include spaces. Also, the number of characters may not be a triangular numberperfect square (Hello World!
):
W
Ho
oller
!dl
A couple more edge cases. 2 letter input ab
:
a
b
And 3 letter input abc
:
a
cb
Procedural Description
In case the above examples aren't clear, here's a procedural description of the process:
- Put down your initial letter.
- Move diagonally down and to the right (i.e., this direction
\
). So if you started at(0,0)
, you will now be at (1,-1). Put down your second letter. - Move left one space a time, dropping a letter on each space, for a total of 3 spaces. That is, drop letters on
(0,-1)
,(-1,-1)
, and(-2, -1)
. - Next move diagonally up and to the right
/
two spaces, dropping letters on(-1,0)
and(0,1)
. - Now cycle back to moving diagonally down and to the right, continuing to step and drop letters as long as your current position is left-right adjacent to an existing letter.
- Next move left again, continuing to step and drop letters as long as you are diagonally adjacent
/
to an existing letter. - Move diagonally up and to the right again
/
, stepping and dropping letters as long as your current position is left-right adjacent to an existing letter. - Repeat steps 5-7 until all letters are used up.
More examples
Rules
- Code golf, standard rules apply.
- Trailing spaces or newlines are ok.
- Consistent leading spaces or newlines are also ok, as long as the shape of the triangle is preserved.