NxM grid or multi-line string (or other reasonable input-format), containing only printable ASCII (unicode range
The amount of closed polygons of the same character that can be found, with two special rules:
- Spaces are wildcards and can be used (multiple times) for any character
0are counted as closed polygons themselves
- (Anti-)Diagonal connections between the same characters (or spaces) are included to form closed polygons.
- You cannot go over other characters (except for the wild-card spaces). (I.e. in the first test case/example below, you cannot form two triangles with the
A's by going over the
x.) So all characters used for a closed polygon should be connected (horizontally, vertically, and/or (anti-)diagonally).
- Polygons are at least three characters (excluding the single characters
- Lines of adjacent characters are not closed polygons.
- The same characters cannot be used for multiple polygons, excluding wildcard spaces.
- Wildcard spaces cannot be counted as
- Three or more spaces alone cannot form a closed polygon. It should always have at least one non-space (and non
- Input can be in any reasonable format. Can be a character-matrix, new-line delimiter string, string-array, character-array with added integer width, etc.
- The inputs will always be an N by M rectangle (or square), so no weird input-shapes
- Since the same characters cannot be used more than once and we want to have as many closed polygons, using multiple characters to form two (or more) closed polygons instead of one larger polygon is of course the intended goal in the counting (which is also why closed polygons formed by
0will never be counted, since they are already closed polygons individually).
- Uppercase and lowercase letters are of course counted as individual characters.
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
- Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Examples / Test Cases:
AAAw AxA4 'AoQ
1822uaslkoo 12*2sl ljoo a* 0a91)j$* ()*#J9dddj* *Q#ID dJj!" *UJD SO&*93
- The yellow one below is not a polygon, because the
o's are already counted as separated polygons
- The purple and brown ones aren't closed
- The red, grey, green, and light-blue use one or more non-space characters that were already used for other closed polygons
Input (dimensions are
3 3 2
AAAA AAAA AAxA
W w Ww
W W WW
abcdQefg hQiQjQQk QlQmnopQ QqrstQQu QvQQQwxy QQz0QQQQ