10
1
There is a virus inside a recipient of 5x5. As we know how it propagates its contamination, your mission is to output the last stage of the contamination.
The recipient
It will be representend as a two dimensional array of 5x5:
0 0 0 0 1
0 0 0 0 1
0 0 0 1 1
0 0 1 1 1
0 1 1 1 1
Where 1
means a position where the virus has already contaminated, and 0
a position not contaminated.
How the virus propagates
- A contaminated position can not be clean.
- A clean position will be contaminated in the next stage only if at least two of its adjacent positions (north, east, south and west cells) are contaminated.
- The last stage of the contamination occurs when no more clean cells can be contaminated.
Sample
Using as as stage 1 of the contamination the recipient described above, the stage 2 will be:
0 0 0 0 1
0 0 0 1 1
0 0 1 1 1
0 1 1 1 1
0 1 1 1 1
The stage 3 of the contamination will be:
0 0 0 1 1
0 0 1 1 1
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
The stage 4 of the contamination will be:
0 0 1 1 1
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
And the stage 5 (in this example, the last one) will be:
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
Challenge
Given as input one stage of the contamination, you should output the last stage of the contamination.
You are allowed to write full program or a function. You can take the input as array/list, as separated numbers, or even as string. Chooses the best way that fits to your language.
The shortest answer in bytes wins!
Another test cases
Input:
1 1 0 0 1
0 0 0 0 0
0 1 0 0 1
0 0 0 0 0
1 0 0 0 1
Output:
1 1 0 0 1
1 1 0 0 1
1 1 0 0 1
1 1 0 0 1
1 1 0 0 1
Input:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output:
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Input:
1 0 0 1 0
0 0 1 0 1
0 0 0 0 0
1 0 0 0 0
0 0 1 0 0
Output:
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Input:
0 1 0 0 0
0 0 0 0 1
0 0 1 0 0
1 0 0 0 0
0 0 0 1 0
Output:
0 1 0 0 0
0 0 0 0 1
0 0 1 0 0
1 0 0 0 0
0 0 0 1 0
1How can
1 0 1
occur in the output? Isn’t the center zero adjacent to two1
s? – Lynn – 2016-03-20T02:19:55.123@Lynn.. I've updated ;) ... sorry for that – removed – 2016-03-20T02:25:32.740
1Could you add
1 0 0 1 0 \ 0 0 1 0 1 \ 0 0 0 0 0 \ 1 0 0 0 0 \ 0 0 1 0 0
as a test case? – Conor O'Brien – 2016-03-20T02:59:52.297@CᴏɴᴏʀO'Bʀɪᴇɴ. Added thanks – removed – 2016-03-20T03:05:38.050
2All the test cases so far have only full row and columns finishing empty. I'd suggest
0 1 0 0 0 \ 0 0 0 0 1 \ 0 0 1 0 0 \ 1 0 0 0 0 \ 0 0 0 1 0
, which stays unchanged. – xnor – 2016-03-20T08:18:51.540@xnor. Sorry for the delay, and thanks for the test case, I've added :) – removed – 2016-03-21T01:14:41.193