How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Wren programming language

Table of Contents

Problem Statement

The purpose of this task is to create a version of an Elementary cellular automaton whose number of cells is only limited by the memory size of the computer. To be precise, consider the state of the automaton to be made of an infinite number of cells, but with a bounded support. In other words, to describe the state of the automaton, you need a finite number of adjacent cells, along with their individual state, and you then consider that the individual state of each of all other cells is the negation of the closest individual cell among the previously defined finite number of cells. Examples: More complex methods can be imagined, provided it is possible to somehow encode the infinite sections. But for this task we will stick to this simple version.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Elementary cellular automaton/Infinite length step by step in the Wren programming language

Source code in the wren programming language

import "./fmt" for Fmt

var addNoCells = Fn.new { |s|
    var l = (s[0] == "*") ? "." : "*"
    var r = (s[-1] == "*") ? "." : "*"
    for (i in 0..1) {
       s.insert(0, l)
       s.add(r)
    }
}

var step = Fn.new { |cells, rule|
    var newCells = []
    for (i in 0...cells.count - 2) {
        var bin = 0
        var b = 2
        for (n in i...i + 3) {
            bin = bin + (((cells[n] == "*") ? 1 : 0) << b)
            b = b >> 1
        }
        var a = ((rule & (1 << bin)) != 0) ? "*" : "."
        newCells.add(a)
    }
    return newCells
}

var evolve = Fn.new { |l, rule|
    System.print(" Rule #%(rule):")
    var cells = ["*"]
    for (x in 0...l) {
        addNoCells.call(cells)
        var width = 40 + (cells.count >> 1)
        Fmt.print("$*s", width, cells.join())
        cells = step.call(cells, rule)
    }
}

evolve.call(35, 90)
System.print()


  

You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the PL/I programming language
You may also check:How to resolve the algorithm Day of the week step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm File size distribution step by step in the zkl programming language
You may also check:How to resolve the algorithm Even or odd step by step in the SSEM programming language
You may also check:How to resolve the algorithm First-class functions/Use numbers analogously step by step in the Elena programming language