How to resolve the algorithm One-dimensional cellular automata step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm One-dimensional cellular automata step by step in the Rust 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 Rust programming language
Source code in the rust programming language
fn get_new_state(windowed: &[bool]) -> bool {
match windowed {
[false, true, true] | [true, true, false] => true,
_ => false
}
}
fn next_gen(cell: &mut [bool]) {
let mut v = Vec::with_capacity(cell.len());
v.push(cell[0]);
for i in cell.windows(3) {
v.push(get_new_state(i));
}
v.push(cell[cell.len() - 1]);
cell.copy_from_slice(&v);
}
fn print_cell(cell: &[bool]) {
for v in cell {
print!("{} ", if *v {'#'} else {' '});
}
println!();
}
fn main() {
const MAX_GENERATION: usize = 10;
const CELLS_LENGTH: usize = 30;
let mut cell: [bool; CELLS_LENGTH] = rand::random();
for i in 1..=MAX_GENERATION {
print!("Gen {:2}: ", i);
print_cell(&cell);
next_gen(&mut cell);
}
}
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the PASM programming language
You may also check:How to resolve the algorithm Cut a rectangle step by step in the Wren programming language
You may also check:How to resolve the algorithm Solve a Numbrix puzzle step by step in the Julia programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the C++ programming language