13
Challenge
Given an ASCII art shape, you must find out whether the shape is a regular hexagon or not.
Hexagons
A regular hexagon is defined using two rules:
- It has six sides
- Each side has equal numbers of characters
For example, the following shape is regular but it doesn't have six sides and so is not a regular hexagon:
B a a h
s a i d
y o u r
g a o t
Similarly, the following shape has six sides but has sides of different lengths and so is not a regular hexagon:
* *
* * *
* * * *
* * *
* *
However, the following shape has six sides and all of the sides have the same number of characters, so it is a regular hexagon:
T h e
h e x a
g o n s a
r e c o
m i n
Rules
Output should be a truthy or a falsey value (truthy if the shape is a regular hexagon, falsey if not).
The shapes will only ever contain printable ASCII characters.
The border of the shape will never contain spaces. If it does, you can return falsey.
There can be arbitrary amounts of whitespace before and/or after the shape.
All angles in the shape may not be equal for example, the following shape is valid input:
. . .
. . .
. . . . .
. . . .
. . .
It will return a falsey value.
All shape inputs will be on a space separated grid. Hexagonal input will be on a staggered grid (each line is offset from the next).
Examples
Truthy
The following shapes should return truthy values:
# _
+ + +
9 :
5 6 7
8 9 0 1
2 3 4 5 6
7 8 9 0
1 2 3
t h i s
i s
a h
e x
a g
o n
! ! ! !
5 6 7
8 9 0 1
2 3 4 5 6
7 8 9 0
1 2 3
Falsey
The following should return falsey values
r e c t a
n g l e s
h e l l o
w o r l d s
t h i s i s b
e t a d e c
a y n o w
*
* *
* * *
.....
.......
.........
.......
.....
This shape is not on a space separated grid and is not staggered.
* * * *
---------
* * * * * *
-------------
* * * * * *
---------
* * * *
5 6 7
8 9 0 1
2 3 4 5 6
7 8 9 0
1 2 3
For single character inputs, your program may output either truthy or falsey:
&
Winning
The shortest program in bytes wins.
4Mathematica one liner incoming. – ThreeFx – 2016-09-09T09:48:02.513
1I think you might want to add two test cases: One without leading spaces, for example:
5 6 7\n8 9 0 1\n2 3 4 5 6\n7 8 9 0\n1 2 3
and one with an additional space leading for one of the rows:ss5 6 7\nss8 9 0 1\n2 3 4 5 6\ns7 8 9 0\nss1 2 3
(leading spaces are replaced withs
to make it a bit more clear in this unformatted form). All 10 of your test cases are validating correctly with my code currently, but these two cases would fail with the approach I used. – Kevin Cruijssen – 2016-09-09T10:02:06.017Is the example with the periods necessarily falsey ? The missing period is a space which is one of the valid characters that can make up the hexagon, – Ton Hospel – 2016-09-09T11:33:18.240
1@TonHospel I think the idea is that the outline is an unbroken regular hexagon. – Martin Ender – 2016-09-09T11:34:51.967
All angles in the shape may not be equal for example, the following shape is valid input: This phrasing seems misleading. Surely we are detecting regular hexagons? Do you mean to write that the symbols don’t necessarily have angular symmetry? – Lynn – 2016-09-09T11:38:30.307
@Lynn It's more that all input will not necessarily be a regular shape – Beta Decay – 2016-09-09T11:41:05.050
Ah. That seems like a strange thing to specify. Anyway, I agree with Ton Hospel’s complaint. You need to specify that we’re interested in non-space printable ASCII characters – Lynn – 2016-09-09T11:43:34.063
@Lynn That isn't the case, though as you have spaces in the ring (the third truthy example) – Beta Decay – 2016-09-09T11:45:06.660
Okay, then you need to specify that we’re interested in hexagons with a space-free border, like Martin says. – Lynn – 2016-09-09T11:49:20.210
What about input with a single non-space but also a non-zero number of spaces (and/or newlines) ? (so not *JUST* a single character) – Ton Hospel – 2016-09-09T12:04:58.680
@TonHospel Well those are leading/trailing spaces, so it's stilk effectively a single character – Beta Decay – 2016-09-09T12:16:04.583
the 3rd truthy example shows just the outline of the hexagon, which depending on your interpretation does not comply with the rule "The shapes will only ever contain printable ASCII characters." What should we do with a shape that has a hexagonal outline and is partially filled in, for example it also has a diagonal? Or a hexagon that is solidly filled on one side and just an outline on the other? – Level River St – 2016-09-09T18:02:25.100
@LevelRiverSt You can expect a hexagon like http://pastebin.com/kcFRBbgi
– Beta Decay – 2016-09-09T18:19:23.230"All shape inputs will be on a space separated grid" conflicts with test case labeled "This shape is not on a space separated grid." – DLosc – 2016-09-09T22:20:36.237
Some more falsey test cases: http://pastebin.com/fHnfHPiJ
– DLosc – 2016-09-10T02:20:26.153