11
Background
An atomic bomb has exploded near the fencepost factory! Since fenceposts are essential for the survival of our civilization, we must rescue as many as possible. We are sending radiation-resistant robots to search the area, and it is your task to program their artificial vision.
Input
Your input is a rectangular grid of the characters |-#
, representing two kinds of fencepost parts and rubble, given as a newline-delimited string.
The fenceposts have been horribly maimed by the explosion, and some have even been fused together by the heat.
We define a candidate fencepost to be either a horizontal pattern that starts and ends in |
and has one or more -
s between them (like |-|
or |---|
but not ||
), or a vertical pattern that starts and ends in -
and has one or more |
s between them (essentially a horizontal candidate fencepost rotated 90 degrees).
An actual fencepost is a candidate fencepost which does not share any of its parts with another candidate fencepost.
Output
Your output is the number of actual fenceposts in the input grid.
Example
Consider the input grid
|#|-|#|##
#||--|||#
###|#|--#
###-||--|
-#-|#-|#-
#---#-#-|
#-#-|#--|
There are 5 candidate fenceposts in this grid, but only 2 of them are actual fenceposts (shown in bold).
Thus the correct output is 2
.
Rules
You can write either a full program or a function. The lowest byte count wins, and standard loopholes are disallowed.
Additional Test cases
Input:
##--
||##
-#|-
|#|#
Output: 0
(0 candidate fenceposts)
Input:
-##|-##--
#|#-|#||-
--|-#|#||
##||||-##
||#|--|-|
-#|-#--|#
|####|#-#
Output: 0
(5 candidate fenceposts)
Input:
#|--|---|-|#
####|##--||-
-|-##||#|---
-#|#-|-#-|||
#|-##--###|-
---#-||-#-##
#|---|###|-#
-|---#-#|#|#
|#--|####-|-
Output: 4
(8 candidate fenceposts)
Input:
-|-||---##|#
---|######-#
####-##|#--|
|||##|#-||||
####-#-||-#|
---#--|#-#--
-|#-####-##-
#||-|-|-###|
|-##||-||-#-
Output: 5
(7 candidate fenceposts)
Input:
|#-|#|#-###|#-#
----#####|#----
|#--#-##-#|##-|
|#-|#-|--##||--
||#-#---##|||##
#|#-|||-#-#--||
--#|-||#|--|#|#
-#-|###||-#--#|
-#||-|#-##||##|
|--|-|||-##|-#|
Output: 4
(9 candidate fenceposts)
Input:
||-|-|-##-#-#-|#--#-
-##|-#|-#-##-|#|--|-
||#---###||-|#|##|-#
#||-#-#-|---|#||#|##
-#-###-##-|-#-|-#||#
##-#--#||-----||-|##
-|--#-#-#|---#-#|#-#
###|-|#-#|-|###|#-|#
#||#-#|--|-||#------
||##|-||#-|--#|#-#-#
|#-|-#-|-|--##-#|||#
-|#--#||##---##|-##-
#|-|-|-||-||#-#-|##-
|-#|##|-|#|#|--||#--
Output: 9
(30 candidate fenceposts)
So are the periods in the
|--...--|
pattern wildcards? Does that mean horizontal fences have to have a t least 5 hyphens? I'm a bit confused by the notation. – BMac – 2015-05-21T17:00:41.363@BMac They are supposed to be an ellipsis, meaning that some hyphens are omitted. I agree that it's not the clearest notation. Let me think of something less ambiguous. – Zgarb – 2015-05-21T17:02:18.737
If we write a function, may it take one 2D-array argument as input, eg.
[[-,|,-],[#,#,-],[-,-,|]]
? – blutorange – 2015-05-21T18:59:41.400@blutorange No, in this challenge it needs to be a single string. – Zgarb – 2015-05-21T19:07:28.923