I'm trying to implement an APL program (ClosedSeg) that could count the total amount of isolated square segments in an array of Boolean. For example:
arr1
1 1 1 0 0 0 0
1 0 1 0 0 1 1
1 1 1 0 0 1 1
0 0 0 0 0 0 0
0 0 0 1 1 1 1
ClosedSeg arr1 ⍝ Four 1's border (non-linear) are minimum to be considered a segment.
2
arr2
1 1 1 1 0 0 0 0 0
1 0 0 1 0 1 1 1 0
1 1 1 1 0 1 0 1 0
1 1 0 0 0 1 1 1 1
0 0 0 0 0 0 1 0 1
0 0 0 0 0 0 1 1 1
ClosedSeg arr2 ⍝ Adjacent isolated segments counts.
4
arr3
1 1 1 1 0 0 0 0 0
1 0 0 1 0 1 1 1 0
1 0 1 1 0 1 0 1 1
1 1 1 0 0 1 1 0 1
0 0 0 0 0 0 1 0 1
0 0 1 0 0 0 1 1 1
0 1 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0
ClosedSeg arr3 ⍝ Any segment shapes are acceptable, note that diagonal 1's is not a segment border.
2
I'm stuck at this.