Find the Fenceposts

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)

Zgarb

Posted 2015-05-21T16:41:04.203

Reputation: 39 083

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

Answers

3

Ruby, 266 268 bytes

To get this started. Makes use of the fact that variables point to objects (a 1-char string for each element of the 2D array) to eliminate overlapping candidates.

Eg. if you execute a="s";b=a, both a and b point to the same string. "test"=="test" returns true, but "test".equal?("test") returns false, because we have created two different String objects.

->d{c=->d,f,v,u{i=0
r=[]
d.map{|y|j=0
e=[]
y.map{|x|f[j]||=[]
f[j][i]=x
x==v ?e[1]?r<<e+[x]&&e=[x]:e[0]?e=[x]:e<<x :x==u&&e[0]?e<<x :e=[]
j+=1}
i+=1}
r}
y=c[d.split.map(&:chars),f=[],?|,?-]+c[f,[],?-,?|]
y.count{|x|y.all?{|q|x.equal?(q)||!(q+x).uniq!(&:object_id)}}}

Test cases on ideone.

blutorange

Posted 2015-05-21T16:41:04.203

Reputation: 1 205

1you can use map instead of each – Cristian Lupascu – 2015-05-22T10:02:46.547

@w0lf thanks, applied. funny thing is, I should have known that... – blutorange – 2015-05-22T13:22:09.023