17
1
Challenge
Given a graphical input of a shape, determine how many holes there are in it.
Not Duplicate
This question was marked as a possible duplicate of Count Islands. I believe this challenge is different from the Count Island challenge because in this one, you have to figure out how to eliminate blocks that touch the border.
Input
Input will be given as some 2D form of input, either a multiline string, an array of strings, or an array of character arrays. This represents the shape. The shape is guaranteed to be in only one piece, connected by edge. Please specify how you want input to be taken.
Output
Output is a single integer stating how many holes there are in the shape. A trailing newline is permitted, but no other leading or trailing whitespace. In other words, the output must match the regular expression ^\d+\n?$
.
What is a hole?
These are single holes:
####
# #
# #
####
####
# #
# ##
###
#####
# # #
# #
#####
These are not holes:
########
########
# ####
# ####
# ######
#
########
###
#
###
##########
#
# ########
# # #
# # #### #
# # ## #
# ###### #
# #
##########
Pretty much, if it the gap joins the outside edge, it is not a hole.
Test cases
#####
# # # -> 2
#####
#####
#
# ### -> 1
# # #
#####
####
## # -> 1 (things are connected by edges)
# ##
####
###
### -> 0 (You must handle shapes with no holes, but input will always contain at least one filled space)
###
You can use any character in place of the '#', and in place of the spaces.
Objective Scoring Criteria
The score is given as the number of bytes in your program.
Winning
The winner will be the submission with the lowest score, by April 4th.
1Related – VisualMelon – 2017-03-24T13:24:06.170
2Could you add
###|# #|##
as a test case? That should be0
, right? – Martin Ender – 2017-03-24T13:41:39.0871Related – Matthew Roh – 2017-03-24T16:20:24.180
1
Possible duplicate of Code-Golf: Count Islands
– Matthew Roh – 2017-03-25T04:00:16.057@SIGSEGV Thank you for pointing that out; however, I believe that this challenge has a critical component that makes it different enough from the other challenge to warrant its own post (I edited in the difference). Please let me know what you think, and we may want to start a discussion in chat if necessary. – HyperNeutrino – 2017-03-25T04:22:50.330
@HyperNeutrino It seems like just detecting spaces instead of islands – Matthew Roh – 2017-03-25T04:57:54.480
@SIGSEGV related, but definitely not a duplicate. Finding closed areas is certainly not the same thing as finding connected points. – Dada – 2017-03-25T19:25:13.120
@SIGSEGV The main (and realistically only) difference is that holes touching the edge don't count here, but islands touching the edge there do count. – HyperNeutrino – 2017-03-25T23:00:45.733
Please explain what you mean by "connected by edge". I would expect this to refer to two cells which are directly above/below, or to the left/right of each other, but in the third test case, it appears that you refer to two cells sharing only a corner this way. – feersum – 2017-03-26T05:19:17.203
@feersum That is to say, two blocks are considered connected if they share an edge (above/below or left/right), so in the third test case, the two cells share only a corner, which means that they are not connected, which means that there is only 1 hole instead of the 2 that some people might expect. – HyperNeutrino – 2017-03-26T05:23:18.057
Um... if there are two empty spaces and they are not connected, how is there not at least two holes? – feersum – 2017-03-26T05:33:38.970
@feersum No, I meant the two filled spaces aren't connected. So like that means that the empty spaces are the same hole. Sorry. – HyperNeutrino – 2017-03-26T06:31:35.763