// Package rectangles find rectangles in ASCII diagrams.
package rectangles
const corner = '+'
var (
cHorizontal = []byte{corner, '-'}
cVertical = []byte{corner, '|'}
)
// Count returns the number of rectangles found in input ASCII diagram.
func Count(input []string) int {
ret := 0
for j, line := range input {
for i, ch := range line {
if ch == corner {
// we are on the top-left corner of a possible rectangle
for right := i + 1; right < len(line) &&
continous(line[right], cHorizontal); right++ {
// we are going right on valid edges, searching for next corner
if line[right] == corner {
// we are on top-right corner of a possible rectangle
for down := j + 1; down < len(input) &&
continous(input[down][i], cVertical) &&
continous(input[down][right], cVertical); down++ {
// we are going down on valid edges (left and right both)
if input[down][i] == corner &&
input[down][right] == corner {
// we have a corner on both left and right position
// so this is a rectangle
ret++
}
}
}
}
}
}
}
return ret
}
// continous checks in an edge is valid.
func continous(in byte, ok []byte) bool {
for _, r := range ok {
if in == r {
return true
}
}
return false
}