12
2
Since tomorrow is the 4th of May, here's a little Star Wars themed post to prepare you mentally to all the bad jokes coming tomorrow.
BACKSTORY
During a session of the galactic senate all the senators are sitting in an n*n
grid.
A sudden outbreak of JarJar flu (which lasts forever and causes the infected to speak like JarJar Binks) causes some of the senators to get infected.
Here's an example with a 6*6
grid where the X
are the infected senators, the corresponding list is : [[0,5],[1,4],[2,3],[2,1],[3,3],[3,0],[4,5],[0,5]]
:
After that the infection begins to spread step by step. Two senators are adjacent if they share a whole edge on the grid (i.e., top,bottom,right,left), which means we exclude diagonals.
We can conclude a senator can be adjacent to 2,3 or 4 other senators and claim the following rules for the infection :
- A senator that has been infected stays infected forever
- A senator is infected at a step if he was adjacent to 2 or more infected senator at the previous step
Here's an example with the previous grid which shows the 2 first steps of the infection :
After the nexts steps all the senate will be infected
YOUR TASK
Your code doesn't need to handle invalid inputs like a list greater than n*n
or coordinates that aren't distincts.
Your code will take as input a list of couples of integers (or a binary grid or any other format that suits your language) and an integer n
(which can be unnecessary if you use another format than a list) , for instance :
8 [[1,2],[1,1],[7,4],[2,7],[4,3]]
n being the side of the grid which means the grid will be a n*n grid, and the list of couples of integers being the coordinates of the cells of the initally infected senators.
The bottom left of the grid is [0,0] and the top right is [n-1,n-1]. The top left is [0,n-1].
Your code must output an integer :
-1
or a falsy value or an error if the whole grid will never be totally infected
or the minimum number of steps required to infect the whole grid
Test cases
6 [[0,5],[1,4],[2,3],[2,1],[3,3],[3,0],[4,5],[5,0]] => 7
4 [[1,1][0,3][1,0][3,0][3,3]] => 9
Remember that this is code-golf, thus the shortest answer in bytes wins !
1Related post on Mathematica.SE. – Martin Ender – 2017-05-03T18:22:13.183
Related – Beta Decay – 2017-05-03T18:41:53.563
What's the minimum value of
n
? Is there a max value? – mbomb007 – 2017-05-03T18:44:11.173@mbomb007 there's no max value in theory but it should be computable. For the minimum value I'd say 1 which outputs 0 or -1 – None – 2017-05-03T18:46:59.457
2Looks like a job for Mathematica's
CellularAutomaton
... – mbomb007 – 2017-05-03T18:48:35.243@mbomb007 See the post I linked in the first comment.
ListConvolve
is probably shorter. – Martin Ender – 2017-05-03T18:54:50.903@Antoine Can we use 1-based indexing for number of generations, i.e. 1=senate is already fully infected, 2=fully infected one step later, 3=... And then 0=infection never takes over? Is this ok? – Adám – 2017-05-03T18:57:47.397
@Adám Well I don't know if it's fair for those who are using the 0-indexing but personally I don't mind – None – 2017-05-03T19:02:31.597
Minecraft reference: a water will be a full block of water if there are two adjacent full blocks (provided that the well is 1 block deep). – Leaky Nun – 2017-05-04T03:50:42.137