One-dimensional cellular automata: Difference between revisions

no edit summary
(add m4)
No edit summary
Line 1,040:
a |= endvals
a = ((a&((a<<1) | (a>>1))) ^ ((a<<1)&(a>>1))) & endmask</lang>
 
=={{header|R}}==
 
<lang R>
 
set.seed(15797, kind="Mersenne-Twister")
 
maxgenerations = 10
cellcount = 20
offendvalue = FALSE
 
## Cells are alive if TRUE, dead if FALSE
universe <- c(offendvalue,
sample( c(TRUE, FALSE), cellcount, replace=TRUE),
offendvalue)
 
## For Boolean values
## Dead if they sum to 0,1,3
## 000 = 0
## 010, 100, 001 = 1
## 111 = 3
## Alive if they sum to 2
## 110, 101, 011 = 2
deadOrAlive <- function(x) sum(x) == 2
 
deadOrAlive2string <- function(x) {
paste(ifelse(x, '#', '_'), collapse="")
}
 
for (x in 1:maxgenerations) {
universe <- c(offendvalue,
apply(embed(universe, 3), 1, deadOrAlive),
offendvalue)
cat(format(x, width=3), deadOrAlive2string(universe), "\n")
}
</lang>
 
Sample output,
<pre>
1 _##_____####_#___#_#__
2 _##_____#__##_____#___
3 _##________##_________
4 _##________##_________
5 _##________##_________
6 _##________##_________
7 _##________##_________
8 _##________##_________
9 _##________##_________
10 _##________##_________
</pre>
 
 
=={{header|Ruby}}==
Anonymous user