Regular ASCII Polygons There are 4 regular polygons that we can construct using ASCII art:


                              . . .
                  . . .      . . . .
  .     . . .    . . . .    . . . . .
 . .    . . .   . . . . .   . . . . .
. . .   . . .    . . . .    . . . . .
                  . . .      . . . .
                              . . .


That is, a triangle, a square, a hexagon and an octagon.

Your task is to take two inputs and produce the corresponding ASCII art polygon.

Input

The two inputs will be:


A string/list of printable ASCII characters (i.e. with code points in the range \$33\$ (!) to \$126\$ (~)
An indicator as to which polygon you should output. This indicator can be any \$4\$ distinct and consistent values (e.g. 1234, ABCD, etc.), but each must be only one character long, to avoid any exploitation of this input.


The string will be a perfect fit for the polygon, meaning that, if the shape to be outputted is:


A triangle, then the length of the string will be a triangular number
A square, then the length of the string will be a square number
A hexagon, then the length of the string will be a centered hexagonal number
An octagon, then the length of the string will be an octo number


The string may have repeated characters, but will always fit the appropriate lengths for the given shape/sequence, so there is no need to worry about too many/few characters.

Output

You should output a shape, indicated by the second input, in the format shown at the top of the challenge with the . replaced with the characters in the inputted string. Each character must be separated by a single space, and leading/trailing whitespace is acceptable, so long as the shape is preserved. The characters may be used in any order, but each character in the string must only appear once. For example, for a hexagon using ABCDABC, the following are valid:


 A B     B A
C D A   A B C
 B C     D C


as each character from the string is used once. However,


 A A    A B    AD
A A A  A B C   ABC
 A A    C A    BC


are not valid (the first only has A, the second doesn't have a D and the third doesn't have spaces between characters).

This is code-golf so the shortest code in bytes wins

Test cases

These test cases use T, S, H and O to indicate which shape the output should be in. The string of characters is on the first line and the indicator on the second

i1+K8{^wsW
O

 i 1
+ K 8
{ ^ w
 s W


,r1~vi4l{W@+r&lt;vzP;mq&gt;:8gJcgg$j`$RFhDV
H

   , r 1 ~
  v i 4 l {
 W @ + r &lt; v
z P ; m q &gt; :
 8 g J c g g
  $ j ` $ R
   F h D V


0J++
S

0 J
+ +


Kn[Vyj
T

  K
 n [
V y j


#5~m
S

# 5
~ m


'*g(AUFV~Ou2n=172s'|S11q&amp;j=+#
O

  ' * g
 ( A U F
V ~ O u 2
n = 1 7 2
s ' | S 1
 1 q &amp; j
  = + #
Ruby, 139 138 bytes



-&gt;s,n{w=(((s=s.chars*' ').size/n=n.hex)**0.5).to_i+k=1&amp;~n
(0..h=w+n/5*~-w).map{|i|s.slice!(0,[w+n/6*~-w,w**k+i,w+h+~i].min*2).center w*4}}


Try it online!

A function taking 2 arguments: a string s and a shape n as a single character in hexadecimal of value 1,2,6 or A. It returns an array of strings.

Explanation

The input string is converted to an array, then joined back into a string with spaces between the characters. The length of this combined string is twice the length of the original string, minus 1. This is divided by a constant, square rooted and rounded to determine the side length w of the polygon to be drawn. The value of the constant is per the table below. For triangles, the square root is rounded down. For others, it needs to be rounded up, so we round down then add k=1&amp;~n to correct.

 Shape   Triangle  Square  Hexagon  Octagon
 n =     1         2       6        10


We iterate through h rows of output. For the triangle and square, h=w.  For the other shapes we need to add w-1 for the hexagon and twice this amount for the octagon. This is done by adding n/5*~-w.

Each line of output is centred and padded to w*4 characters which is enough for all cases. Chunks of s are removed using slice! and outputted. For the octagon the number of symbols to be outputted on each line is the minimum of three numbers: w+w-1 for the centre section, w+i for the top section and w+h-i-1 = w+h+~i for the bottom section. These formulas also work for the hexagon. Note that the numbers have to be multiplied by 2 to account for the spaces.

For the square and triangle, the width of the output is restricted to w. Therefore the formula for the centre section becomes w+n/6*~-w to cover all shapes . For the triangle, the width of the output is further restricted to 1+i. Therefore the formula for the top section becomes w**k+i to cover all shapes.