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

Published on 12 May 2024 09:40 PM

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

Source code in the oforth programming language

: nextGen( l )
| i s |
   l byteSize dup ->s String newSize
   s loop: i [ 
      i 1 if=: [ 0 ] else: [ i 1- l byteAt '#' = ]
      i l byteAt '#' = + 
      i s if=: [ 0 ] else: [ i 1+ l byteAt '#' = ] + 
      2 if=: [ '#' ] else: [ '_' ] over add
      ]
;
 
: gen( l n -- )
    l dup .cr #[ nextGen dup .cr ] times( n ) drop ;

  

You may also check:How to resolve the algorithm Call a function step by step in the Swift programming language
You may also check:How to resolve the algorithm HTTPS/Client-authenticated step by step in the Racket programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Raku programming language
You may also check:How to resolve the algorithm Conway's Game of Life step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Substring/Top and tail step by step in the PARI/GP programming language