One-dimensional cellular automata: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1,044:
 
<lang R>
 
set.seed(15797, kind="Mersenne-Twister")
 
Line 1,056 ⟶ 1,055:
offendvalue)
 
## List of patterns in which the cell stays alive
## For Boolean values
stayingAlive <- lapply(list(c(1,1,0),
## Dead if they sum to 0,1,3
offendvalue c(1,0,1),
## 000 = 0
c(0,1,0)), as.logical)
## 010, 100, 001 = 1
 
## 111 = 3
## Alivex if: theylength sum3 tological 2vector
## map: list of length 3 logical vectors that map to patterns
## 110, 101, 011 = 2
## in which x stays alive
deadOrAlive <- function(x, map) sumlist(x) ==%in% 2map
 
cellularAutomata <- function(x, map) {
c(x[1], apply(embed(universex, 3), 1, deadOrAlive, map=map), x[length(x)])
}
 
deadOrAlive2string <- function(x) {
Line 1,069 ⟶ 1,073:
}
 
for (xi in 1:maxgenerations) {
universe <- ccellularAutomata(offendvalueuniverse, stayingAlive)
cat(format(xi, width=3), deadOrAlive2string(universe), "\n")
apply(embed(universe, 3), 1, deadOrAlive),
offendvalue)
cat(format(x, width=3), deadOrAlive2string(universe), "\n")
}
</lang>
Anonymous user