A string whose length is a positive triangular number (1, 3, 6, 10, 15...) can be arranged into an "equilateral text triangle" by adding some spaces and newlines (and keeping it in the same reading order).
For example, the length 10 string ABCDEFGHIJ
becomes:
A
B C
D E F
G H I J
Write a program or function that takes in such a string, except it will only contains the characters 0
and 1
. (You may assume the input is valid.)
For the resulting "equilateral text triangle", output (print or return) one of four numbers that denotes the type of symmetry exhibited:
Output
2
if the triangle has bilateral symmetry. i.e. it has a line of symmetry from any one corner to the opposite side's midpoint.Examples:
0 1 1 1 0 1 0 0 1 0 1 0 1 1 1 1 0 1 0 1 1 1
Output
3
if the triangle has rotational symmetry. i.e. it could be rotated 120° with no visual change.Examples:
0 1 0 0 1 1 0 1 0 0 0 0 1 1 0 0 0 0 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 1 0 0 0 1 0 0 0 0 1 0 0 1 1 1
Output
6
if the triangle has both bilateral and rotational symmetry. i.e. it matches the conditions for outputting both2
and3
.Examples:
0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0
Output
1
if the triangle has neither bilateral nor rotational symmetry.Examples:
1 1 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1
The shortest code in bytes wins. Tiebreaker is earlier answer.
Aside from an optional trailing newline, the input string may not have space/newline padding or structure - it should be plain 0
's and 1
's.
If desired you may use any two distinct printable ASCII characters in place of 0
and 1
.
Test Cases
Taken direct from examples.
011 -> 2
101 -> 2
001010 -> 2
1111010111 -> 2
0100110100 -> 3
0011000010 -> 3
101111111010111 -> 3
101001100010000100111 -> 3
0 -> 6
1 -> 6
000 -> 6
100101 -> 6
0000100000 -> 6
110000 -> 1
001101 -> 1
1101111111 -> 1
111111000111111 -> 1
"Rotating" any input by 120° will of course result in the same output.