How to resolve the algorithm One-dimensional cellular automata step by step in the Ceylon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm One-dimensional cellular automata step by step in the Ceylon 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 Ceylon programming language

Source code in the ceylon programming language

shared abstract class Cell(character) of alive | dead {
	shared Character character;
	string => character.string;
	shared formal Cell opposite;
}

shared object alive extends Cell('#') {
	opposite => dead;
}
shared object dead extends Cell('_') {
	opposite => alive;
}

shared Map cellsByCharacter = map { for (cell in `Cell`.caseValues) cell.character->cell };

shared class Automata1D({Cell*} initialCells) {
	
	
	value permanentFirstCell = initialCells.first else dead;
	value permanentLastCell = initialCells.last else dead;
	
	value cells = Array { *initialCells.rest.exceptLast };
	
	shared Boolean evolve() {
		
		value newCells = Array {
			for (index->cell in cells.indexed)
			let (left = cells[index - 1] else permanentFirstCell, 
				right = cells[index + 1] else permanentLastCell,
				neighbours = [left, right], 
				bothAlive = neighbours.every(alive.equals),
				bothDead = neighbours.every(dead.equals))
			if (bothAlive)
			then cell.opposite
			else if (cell == alive && bothDead)
			then dead
			else cell
		};
		
		if (newCells == cells) {
			return false;
		}
		
		newCells.copyTo(cells);
		return true;
	}
	
	string => permanentFirstCell.string + "".join(cells) + permanentLastCell.string;
}

shared Automata1D? automata1d(String string) => 
		let (cells = string.map((Character element) => cellsByCharacter[element]))
		if (cells.every((Cell? element) => element exists)) 
		then Automata1D(cells.coalesced) 
		else null;

shared void run() {

	assert (exists automata = automata1d("__###__##_#_##_###__######_###_#####_#__##_____#_#_#######__"));
	
	variable value generation = 0;
	print("generation ``generation`` ``automata``");
	while (automata.evolve() && generation<10) {
		print("generation `` ++generation `` ``automata``");
	}
}


  

You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Haskell programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Oz programming language
You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the C++ programming language
You may also check:How to resolve the algorithm Wordle comparison step by step in the Java programming language
You may also check:How to resolve the algorithm Formatted numeric output step by step in the Icon and Unicon programming language