How to resolve the algorithm One-dimensional cellular automata step by step in the Icon and Unicon programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm One-dimensional cellular automata step by step in the Icon and Unicon programming language
Table of Contents
Problem Statement
Assume an array of cells with an initial distribution of live and dead cells, and imaginary cells off the end of the array having fixed values. Cells in the next generation of the array are calculated based on the value of the cell and its left and right nearest neighbours in the current generation. If, in the following table, a live cell is represented by 1 and a dead cell by 0 then to generate the value of the cell at a particular index in the array of cellular values you use the following table:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm One-dimensional cellular automata step by step in the Icon and Unicon programming language
Source code in the icon programming language
# One dimensional Cellular automaton
record Automaton(size, cells)
procedure make_automaton (size, items)
automaton := Automaton (size, items)
while (*items < size) do push (automaton.cells, 0)
return automaton
end
procedure automaton_display (automaton)
every (write ! automaton.cells)
end
procedure automaton_evolve (automaton)
revised := make_automaton (automaton.size, [])
# do the left-most cell
if ((automaton.cells[1] + automaton.cells[2]) = 2) then
revised.cells[1] := 1
# do the right-most cell
if ((automaton.cells[automaton.size] + automaton.cells[automaton.size-1]) = 2) then
revised.cells[revised.size] := 1
# do the intermediate cells
every (i := 2 to (automaton.size-1)) do {
if ((automaton.cells[i-1] + automaton.cells[i] + automaton.cells[i+1]) = 2) then
revised.cells[i] := 1
}
return revised
end
procedure main ()
automaton := make_automaton (20, [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0])
every (1 to 10) do { # generations
automaton_display (automaton)
automaton := automaton_evolve (automaton)
}
end
procedure main(A)
A := if *A = 0 then ["01110110101010100100"]
CA := show("0"||A[1]||"0") # add always dead border cells
every CA := show(|evolve(CA)\10) # limit to max of 10 generations
end
procedure show(ca)
write(ca[2:-1]) # omit border cells
return ca
end
procedure evolve(CA)
newCA := repl("0",*CA)
every newCA[i := 2 to (*CA-1)] := (CA[i-1]+CA[i]+CA[i+1] = 2, "1")
return CA ~== newCA # fail if no change
end
You may also check:How to resolve the algorithm Empty program step by step in the Odin programming language
You may also check:How to resolve the algorithm Emirp primes step by step in the Phix programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the zkl programming language
You may also check:How to resolve the algorithm Topological sort step by step in the C++ programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Kotlin programming language