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

Published on 12 May 2024 09:40 PM
#D

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

Source code in the d programming language

void main() {
   import std.stdio, std.algorithm;

   enum nGenerations = 10;
   enum initial = "0011101101010101001000";
   enum table = "00010110";

   char[initial.length + 2] A = '0', B = '0';
   A[1 .. $-1] = initial;
   foreach (immutable _; 0 .. nGenerations) {
      foreach (immutable i; 1 .. A.length - 1) {
         write(A[i] == '0' ? '_' : '#');
         const val = (A[i-1]-'0' << 2) | (A[i]-'0' << 1) | (A[i+1]-'0');
         B[i] = table[val];
      }
      A.swap(B);
      writeln;
   }
}


void main() {
    import std.stdio, std.algorithm, std.range;

    auto A = "_###_##_#_#_#_#__#__".map!q{a == '#'}.array;
    auto B = A.dup;

    do {
        A.map!q{ "_#"[a] }.writeln;
        A.zip(A.cycle.drop(1), A.cycle.drop(A.length - 1))
        .map!(t => [t[]].sum == 2).copy(B);
        A.swap(B);
    } while (A != B);
}


void main() {
    import std.stdio, std.algorithm, std.range, std.bitmanip;

    immutable initial = "__###_##_#_#_#_#__#___";
    enum nGenerations = 10;
    BitArray A, B;
    A.init(initial.map!(c => c == '#').array);
    B.length = initial.length;

    foreach (immutable _; 0 .. nGenerations) {
        //A.map!(b => b ? '#' : '_').writeln;
        //foreach (immutable i, immutable b; A) {
        foreach (immutable i; 1 .. A.length - 1) {
            "_#"[A[i]].write;
            immutable val = (uint(A[i - 1]) << 2) |
                            (uint(A[i])     << 1) |
                             uint(A[i + 1]);
            B[i] = val == 3 || val == 5 || val == 6;
        }

        writeln;
        A.swap(B);
    }
}


  

You may also check:How to resolve the algorithm Conditional structures step by step in the XSLT programming language
You may also check:How to resolve the algorithm Morse code step by step in the VBA programming language
You may also check:How to resolve the algorithm Count the coins step by step in the Dart programming language
You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the Ruby programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Swift programming language