| Bytes | Lang | Time | Link |
|---|---|---|---|
| 023 | APL Dyalog Unicode | 160327T100400Z | Ven |
| 236 | Lua | 160322T002540Z | Trebuche |
| 022 | MATL | 160320T035224Z | Luis Men |
| 151 | TIBASIC | 160320T041400Z | Conor O& |
| 087 | Javascript ES6 | 160320T051326Z | nderscor |
| 097 | Python 2 | 160320T030149Z | xsot |
| nan | 160320T024500Z | Calculat |
APL (Dyalog Unicode), 76 72 70 46 23 bytes
{2≤+/⍵[;2],⍵[2;]}⌺3 3⍣≡
Stencil of 3x3 to get every cell "around" the current cell we're one, then sum the 2nd row and the 2nd column. Make sure at least 2 of our "neighbors" are on. Note that this counts us twice in the result, so even if none of our actual neighbors are infected, we still count two infections.
example (function assigned to contaminate):
stage ⍝ the base matrix
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
contaminate stage ⍝ apply the function
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
Lua, 236 bytes
s=arg[1]u=s.sub while p~=s do p=s s=s:gsub("(%d)()",function(d,p)t=0 t=t+((p-2)%6>0 and u(s,p-2,p-2)or 0)t=t+(p-7>0 and u(s,p-7,p-7)or 0)t=t+(p+5<30 and u(s,p+5,p+5)or 0)t=t+(p%6>0 and u(s,p,p)or 0)return t>1 and 1 or d end)end print(s)
Accepts input on the command line, and uses Lua's string manipulation to get the answer.
Ungolfed:
s=arg[1]
u=s.sub
while p~=s do
p=s
s=s:gsub("(%d)()", function(d, p)
t=0
t=t+((p-2)%6>0 and u(s,p-2,p-2)or 0)
t=t+(p-7>0 and u(s,p-7,p-7)or 0)
t=t+(p+5<30 and u(s,p+5,p+5)or 0)
t=t+(p%6>0 and u(s,p,p)or 0)
return t>1 and 1 or d
end)
end
print(s)
MATL, 22 bytes
tn:"t5Bt!=~2X53$Y+1>Y|
This works in current version (15.0.0) of the language.
Input format is: 2D array with rows separated by semicolons. So the four test cases have the following inputs:
[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]
[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]
[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]
[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]
Explanation
This repeatedly performs a 2D convolution of the input array with the following mask, which defines which neighbours count as contaminating:
0 1 0
1 0 1
0 1 0
In order to obtain a result the same size as the original array, it is first padded with a frame of zeros and then only the "valid" part of the convolution is kept (i.e. that without edge effects).
A threshold of 2 is applied to the output of the convolution, and the result is element-wise ORed with the original input.
This must be done a sufficient number of times to ensure the final state has been reached. A simple criterion that fulfills this is: iterate as many times as the number of entries in the input array (that is, 25 times in the test cases).
t % get input 2D array implicitly. Duplicate
n:" % repeat as many times as elements in the input array
t % duplicate array
5B % number 5 in binary: row vector [1 0 1]
t! % duplicate and transpose into column
=~ % compare for inequality with broadcast. Gives desired mask
2X53$Y+ % 2D convolution. Output has same size as input
1> % true for elements equal or greater than 2
Y| % element-wise OR with previous copy of the array
% end loop. Implicitly display
TI-BASIC, 151 bytes
Prompt [A]
[A]→[B]
Lbl 0
[B]→[A]
For(Y,1,5
For(X,1,5
DelVar ADelVar BDelVar CDelVar D
If Y>1:[A](Y-1,X→A
If Y<5:[A](Y+1,X→B
If X>1:[A](Y,X-1→C
If X<5:[A](Y,X+1→D
max(A+B+C+D≥2,[A](Y,X→[B](Y,X
End
End
If [A]≠[B]:Goto 0
[B]
Input as [[1,0,0,1,1][1,0,0,0,0]...].
Javascript (ES6), 91 89 87 bytes
As a function which accepts input as an array of numbers or strings.
-2 bytes from Neil (combining assignment of y with string conversion)
-2 bytes (removing variable j)
f=x=>(y=x.map((z,i)=>~(i%5&&x[i-1])+~(i%5<4&x[i+1])+~x[i-5]+~x[i+5]<-5|z))+[]==x?y:f(y)
<!-- Snippet demo with array <-> string conversion for convenience -->
<textarea id="input" cols="10" rows="5">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</textarea>
<button id="submit">Run</button>
<pre id="output"></pre>
<script>
strToArray=x=>x.match(/\d/g)
arrayToStr=x=>([]+x).replace(/(.),(.),(.),(.),(.),*/g, '$1 $2 $3 $4 $5\n')
submit.onclick=_=>output.innerHTML=arrayToStr(f(strToArray(input.value)))
</script>
Python 2, 97 bytes
s=' '*6+input()
exec"s=s[1:]+('1'+s)[sum(s[i]<'1'for i in[-6,-1,1,6])>2or'/'>s];"*980
print s[6:]
Try it online. Input is taken as a quoted string with each row delimited by newlines. The 980 is not optimal and can be replaced with a lower multiple of 35. Since it has no impact on the length of this program, I have left the determination of the lowest safe upper bound as an exercise for the reader.
Since this is basically talking about a cellular automaton I give you..
Golly Quicklife rule, 10 bytes
01234/234V
Input the rule, paste the grid into Golly, run pattern. The resulting pattern is the output.
Explanation:
01234 Survive on any number of neighbors
/234 Born on >2 neighbors
V Only directly adjacent neighbors count
Or if you insist on a full RuleLoader rule, 89 bytes:
@RULE X
@TABLE
n_states:2
neighborhood:vonNeumann
symmetries:permute
011001
011101
011111
Rulename is X, same steps as before.