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<vzP;mq>:8gJcgg$j`$RFhDV
H
, r 1 ~
v i 4 l {
W @ + r < v
z P ; m q > :
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&j=+#
O
' * g
( A U F
V ~ O u 2
n = 1 7 2
s ' | S 1
1 q & j
= + #
[[0,0,1,0],[0,1,0,1],[1,0,1,0,1]]
for example, and so avoid the need to encode the triangle within code. Or is that ok? \$\endgroup\$eval
. The four distinct inputs just define functions which format the four shapes. @cairdcoinheringaahing, I recommend scoring aslength(code)+length(indicators)
; or restrict indicator to one character. \$\endgroup\$However, I'd expect the community to downvote answers which violate the spirit of the challenge, such as an answer that uses that "loophole", and I certainly would downvote such a boring answer
I really do not think that we should downvote an answer to a code golf question that is both golfed and fits the defined spec! \$\endgroup\$<
and>
characters didn't work with the<pre>
tags. Updated to fix that \$\endgroup\$