How to resolve the algorithm One-dimensional cellular automata step by step in the OCaml programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm One-dimensional cellular automata step by step in the OCaml 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 OCaml programming language
Source code in the ocaml programming language
let get g i =
try g.(i)
with _ -> 0
let next_cell g i =
match get g (i-1), get g (i), get g (i+1) with
| 0, 0, 0 -> 0
| 0, 0, 1 -> 0
| 0, 1, 0 -> 0
| 0, 1, 1 -> 1
| 1, 0, 0 -> 0
| 1, 0, 1 -> 1
| 1, 1, 0 -> 1
| 1, 1, 1 -> 0
| _ -> assert(false)
let next g =
let old_g = Array.copy g in
for i = 0 to pred(Array.length g) do
g.(i) <- (next_cell old_g i)
done
let print_g g =
for i = 0 to pred(Array.length g) do
if g.(i) = 0
then print_char '_'
else print_char '#'
done;
print_newline()
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the RPL programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the C++ programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Simulate input/Keyboard step by step in the VBScript programming language
You may also check:How to resolve the algorithm Topic variable step by step in the Erlang programming language