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

Published on 12 May 2024 09:40 PM

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

Source code in the montilang programming language

30 VAR length .
35 VAR height .
FOR length 0 ENDFOR 1 0 ARR VAR list . 
length 1 - VAR topLen . 
FOR topLen 0 ENDFOR 1 ARR VAR topLst .  

DEF getNeighbors
    1 - VAR tempIndex . 
    GET tempIndex SWAP 
    tempIndex 1 + VAR tempIndex .
    GET tempIndex SWAP 
    tempIndex 1 + VAR tempIndex .
    GET tempIndex SWAP .
    FOR 3 TOSTR ROT ENDFOR
    FOR 2 SWAP + ENDFOR  
ENDDEF

DEF printArr
    LEN 1 - VAR stLen .
    0 VAR j .
    FOR stLen
        GET j 
        TOSTR OUT .
        j 1 + VAR j .
    ENDFOR
    || PRINT .
ENDDEF

FOR height
    FOR length 0 ENDFOR ARR VAR next .
    1 VAR i .
    FOR length
        list i getNeighbors VAR last . 
        i 1 - VAR ind .
        last |111| == 
        IF : .
            next 0 INSERT ind
        ENDIF

        last |110| ==
        IF : .
            next 1 INSERT ind
        ENDIF

        last |101| ==
        IF : .
            next 1 INSERT ind
        ENDIF

        last |100| ==
        IF : .
            next 0 INSERT ind
        ENDIF

        last |011| ==
        IF : .
            next 1 INSERT ind
        ENDIF

        last |010| ==
        IF : .
            next 1 INSERT ind
        ENDIF

        last |001| ==
        IF : .
            next 1 INSERT ind
        ENDIF

        last |000| ==
        IF : .
            next 0 INSERT ind
        ENDIF
        clear
        i 1 + VAR i .
    ENDFOR 
    next printArr .
    next 0 ADD APPEND . VAR list .
ENDFOR

  

You may also check:How to resolve the algorithm Metaprogramming step by step in the Arturo programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Fantom programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Rust programming language
You may also check:How to resolve the algorithm Currying step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Soundex step by step in the VBScript programming language