Count the number of contiguous blocks within a given 3D input.
Input
The input will consist of one or more rectangles of characters separated by blank lines. Each rectangle represents a cross section of the 3D space. The characters used will be 0
, representing empty space, and 1
representing a solid block.
Example
00000
01110
01110
01110
00000
01110
01110
01110
00000
01110
01110
01110
In the above example we have a 3x3 cube - the bottom layer first, then the middle and then the top.
Output
The output should be a single integer giving the number of contiguous blocks within the input.
What's considered contiguous?
If any two 1
s are adjacent to each other to the front, back, left, right, top or bottom, they are considered to be part of the same contiguous block. If they are not adjacent or they are only adjacent diagonally, they are not considered to be part of the same block.
Examples
00
11
Contiguous.
10
10
Contiguous.
10
01
Not contiguous.
01
00
01
00
Contiguous.
01
00
00
01
Not contiguous.
01
00
00
10
Not contiguous.
01
00
10
00
Not contiguous.
Test inputs
11101110111
10000011100
00111000000
00000000000
Result: 1
111
111
111
000
010
000
111
111
111
Result: 1
111
111
111
100
000
000
101
011
111
Result: 2
11011011011
00100100100
11011011011
00100100100
00100100100
11011011011
00100100100
11011011011
Result: 14
11011011011
01100100110
11011011011
00100100100
00100100100
11011011011
00100100100
11011011011
Result: 12
11011011011
00100100100
11011011011
01100100110
00100100100
11011011011
00100100100
11011011011
Result: 10
11111
10001
10001
10001
11111
Result: 1
11111
10001
10101
10001
11111
Result: 2
11111
11111
11111
11111
11111
11111
10001
10001
10001
11111
11111
10001
10001
10001
11111
11111
10001
10001
10001
11111
11111
11111
11111
11111
11111
Result: 1
11111
11111
11111
11111
11111
11111
10001
10001
10001
11111
11111
10001
10101
10001
11111
11111
10001
10001
10001
11111
11111
11111
11111
11111
11111
Result: 2
11111
11111
11111
11111
11111
11111
10001
10001
10001
11111
11111
10001
10101
10001
11111
11111
10001
10101
10001
11111
11111
11111
11111
11111
11111
Result: 1
Input format
I'm happy to be slightly flexible on the input format, so if you want to take a list of strings as input instead of one continuous string that's okay, and if you want to take a list of lists of characters (or integers if that helps) that's okay too.
Your answer can be a full program reading input or a function taking the input as a parameter. If your anonymous function needs a name in order for me to call it, then the characters for assigning the function to a name need to be counted too.
This is code golf so shortest code in bytes will win the big green tick, but any answer that meets the spec, passes the tests and has obviously been golfed will get my upvote.