Elementary cellular automaton/Infinite length: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 4 users not shown)
Line 14:
 
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.
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F step(cells, rule)
V result = ‘’
L(i) 0 .< cells.len - 2
V bin = 0
V b = 2
L(n) i .< i + 3
bin += Int(cells[n] == ‘*’) << b
b >>= 1
V a = I (rule [&] (1 << bin)) != 0 {‘*’} E ‘.’
result ‘’= a
R result
 
F addNoCells(&cells)
V left = I cells[0] == ‘*’ {‘.’} E ‘*’
V right = I cells.last == ‘*’ {‘.’} E ‘*’
cells = left‘’cells‘’right
cells = left‘’cells‘’right
 
F evolve(limit, rule)
print(‘Rule #’rule)
V cells = ‘*’
L 0 .< limit
addNoCells(&cells)
V width = 40 + (cells.len >> 1)
print(cells.rjust(width))
cells = step(cells, rule)
 
evolve(35, 90)</syntaxhighlight>
 
{{out}}
<pre>
Rule #90
..*..
..*.*..
..*...*..
..*.*.*.*..
..*.......*..
..*.*.....*.*..
..*...*...*...*..
..*.*.*.*.*.*.*.*..
..*...............*..
..*.*.............*.*..
..*...*...........*...*..
..*.*.*.*.........*.*.*.*..
..*.......*.......*.......*..
..*.*.....*.*.....*.*.....*.*..
..*...*...*...*...*...*...*...*..
..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..
..*...............................*..
..*.*.............................*.*..
..*...*...........................*...*..
..*.*.*.*.........................*.*.*.*..
..*.......*.......................*.......*..
..*.*.....*.*.....................*.*.....*.*..
..*...*...*...*...................*...*...*...*..
..*.*.*.*.*.*.*.*.................*.*.*.*.*.*.*.*..
..*...............*...............*...............*..
..*.*.............*.*.............*.*.............*.*..
..*...*...........*...*...........*...*...........*...*..
..*.*.*.*.........*.*.*.*.........*.*.*.*.........*.*.*.*..
..*.......*.......*.......*.......*.......*.......*.......*..
..*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*..
..*...*...*...*...*...*...*...*...*...*...*...*...*...*...*...*..
..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..
..*...............................................................*..
..*.*.............................................................*.*..
..*...*...........................................................*...*..
</pre>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <iomanip>
Line 58 ⟶ 130:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 91 ⟶ 163:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.array, std.range, std.typecons, std.string, std.conv,
std.algorithm;
alias R = replicate;
Line 115 ⟶ 187:
}
}
}</langsyntaxhighlight>
The output is the same as the Python entry.
 
Line 121 ⟶ 193:
{{works with|Elixir|1.3}}
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">
defmodule Elementary_cellular_automaton do
def infinite(cell, rule, times) do
Line 152 ⟶ 224:
IO.puts "\nRule : #{rule}"
Elementary_cellular_automaton.infinite("1", rule, 25)
end)</langsyntaxhighlight>
 
{{out}}
Line 213 ⟶ 285:
=={{header|Go}}==
{{trans|C++}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 274 ⟶ 346:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 340 ⟶ 412:
First we provide the datatype, the viewer and constructor:
 
<langsyntaxhighlight Haskelllang="haskell">{-# LANGUAGE DeriveFunctor #-}
 
import Control.Comonad
Line 352 ⟶ 424:
fromList [] = fromList [0]
fromList (x:xs) = let zeros = Inf.repeat 0
in Cells zeros x (xs +++ zeros)</langsyntaxhighlight>
 
In order to run the CA on the domain we make it an instance of <code>Comonad</code> class. Running the CA turns to be just an iterative comonadic ''extension'' of the rule:
 
<langsyntaxhighlight Haskelllang="haskell">instance Comonad Cells where
extract (Cells _ x _) = x
duplicate x = Cells (rewind left x) x (rewind right x)
Line 365 ⟶ 437:
 
runCA rule = iterate (=>> step)
where step (Cells (l ::: _) x (r ::: _)) = rule l x r</langsyntaxhighlight>
 
Following is the rule definition and I/O routine:
 
<langsyntaxhighlight Haskelllang="haskell">rule n l x r = n `div` (2^(4*l + 2*x + r)) `mod` 2
 
displayCA n w rule init = mapM_ putStrLn $ take n result
where result = fmap display . view w <$> runCA rule init
display 0 = ' '
display _ = '*'</langsyntaxhighlight>
 
{{Out}}
Line 422 ⟶ 494:
Implementation:
 
<langsyntaxhighlight Jlang="j">ext9=: (9#1-{.!.1),],9#1-{:!.1
trim=: |.@(}.~ ] i. 1-{.)^:2
next=: trim@(((8$2) #: [) {~ 2 #. 1 - [: |: |.~"1 0&_1 0 1@]) ext9</langsyntaxhighlight>
 
In other words, a wrapped version of the [[Elementary_cellular_automaton#J|original implementation]].
Line 430 ⟶ 502:
example use:
 
<langsyntaxhighlight Jlang="j"> ' *'{~90 next^:(i.9) 1
*
* *
Line 439 ⟶ 511:
* * * *
* * * * * * * *
* *</langsyntaxhighlight>
 
Looks like a [[Sierpinski_triangle|Sierpinski triangle]]
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public final class ElementaryCellularAutomatonInfiniteLength {
 
public static void main(String[] aArgs) {
evolve(35, 90);
System.out.println();
}
private static void evolve(int aLimit, int aRule) {
System.out.println(" Rule# " + aRule);
StringBuilder cells = new StringBuilder(Character.toString(STAR));
for ( int i = 0; i < aLimit; i++ ) {
addCells(cells);
final int width = 40 - ( cells.length() >> 1 );
System.out.println(" ".repeat(width) + cells);
cells = nextStep(cells, aRule);
}
}
private static void addCells(StringBuilder aCells) {
final char left = ( aCells.charAt(0) == STAR ) ? DOT : STAR;
final char right = ( aCells.charAt(aCells.length() - 1 ) == STAR ) ? DOT : STAR;
for ( int i = 0; i < 2; i++ ) {
aCells.insert(0, left);
aCells.append(right);
}
}
private static StringBuilder nextStep(StringBuilder aCells, int aRule) {
StringBuilder nextCells = new StringBuilder();
for ( int i = 0; i < aCells.length() - 2; i++ ) {
int binary = 0;
int shift = 2;
for ( int j = i; j < i + 3; j++ ) {
binary += ( ( aCells.charAt(j) == STAR ) ? 1 : 0 ) << shift;
shift >>= 1;
}
final char symbol = ( ( aRule & ( 1 << binary ) ) == 0 ) ? DOT : STAR;
nextCells.append(symbol);
}
return nextCells;
}
private static final char DOT = '.';
private static final char STAR = '*';
 
}
</syntaxhighlight>
{{ out }}
<pre>
Rule# 90
..*..
..*.*..
..*...*..
..*.*.*.*..
..*.......*..
..*.*.....*.*..
..*...*...*...*..
..*.*.*.*.*.*.*.*..
..*...............*..
..*.*.............*.*..
..*...*...........*...*..
..*.*.*.*.........*.*.*.*..
..*.......*.......*.......*..
..*.*.....*.*.....*.*.....*.*..
..*...*...*...*...*...*...*...*..
..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..
..*...............................*..
..*.*.............................*.*..
..*...*...........................*...*..
..*.*.*.*.........................*.*.*.*..
..*.......*.......................*.......*..
..*.*.....*.*.....................*.*.....*.*..
..*...*...*...*...................*...*...*...*..
..*.*.*.*.*.*.*.*.................*.*.*.*.*.*.*.*..
..*...............*...............*...............*..
..*.*.............*.*.............*.*.............*.*..
..*...*...........*...*...........*...*...........*...*..
..*.*.*.*.........*.*.*.*.........*.*.*.*.........*.*.*.*..
..*.......*.......*.......*.......*.......*.......*.......*..
..*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*.....*.*..
..*...*...*...*...*...*...*...*...*...*...*...*...*...*...*...*..
..*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*..
..*...............................................................*..
..*.*.............................................................*.*..
..*...*...........................................................*...*..
</pre>
 
=={{header|jq}}==
Line 449 ⟶ 610:
 
'''Preliminaries'''
<syntaxhighlight lang="jq">
<lang jq>
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
 
Line 474 ⟶ 635:
# input: an array, e.g. as produced by [7|stream]
# output: the corresponding binary string
def to_b: reverse | map(tostring) | join("") | sub("^0";"");</langsyntaxhighlight>
'''The Cellular Automaton'''
<langsyntaxhighlight lang="jq"># Output: an unbounded stream of the form [count, row]
# giving the rows produced by the eca defined by
# $cells (a string) and $rule (an integer)
Line 494 ⟶ 655:
| .c = ([range(1; .c|length - 1) as $i | $neighbours2next[.c[$i-1:$i+2] ]] | join(""));
[$i, .c] ) ;
</syntaxhighlight>
</lang>
'''The Task'''
<langsyntaxhighlight lang="jq"># $lines specifies the number of lines to display for each eca
def main($lines):
(90, 30) as $rule
Line 504 ⟶ 665:
| ($line|lpad(3)) + " " * ($lines - $line) + (.[1] | tr("01"; ".#") )));
 
main(25)</langsyntaxhighlight>
{{out}}
As for [[#Python|Python]].
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">function ecainfinite(cells, rule, n)
notcell(cell) = (cell == '1') ? '0' : '1'
rulebits = reverse(string(rule, base = 2, pad = 8))
Line 533 ⟶ 694:
 
testinfcells(25)
</langsyntaxhighlight>{{out}}
<pre>
Rule: 90 (01011010)
Line 592 ⟶ 753:
=={{header|Kotlin}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
fun evolve(l: Int, rule: Int) {
Line 632 ⟶ 793:
evolve(35, 90)
println()
}</langsyntaxhighlight>
 
{{out}}
Line 676 ⟶ 837:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
An infinite background is built-in the language:
<langsyntaxhighlight Mathematicalang="mathematica">CellularAutomaton[18, {{1}, 0}, 15] // ArrayPlot
CellularAutomaton[30, {{1}, 0}, 15] // ArrayPlot</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import strutils
 
 
Line 714 ⟶ 875:
 
 
evolve(35, 90)</langsyntaxhighlight>
 
{{out}}
Line 756 ⟶ 917:
=={{header|Perl}}==
The edges of a pattern is implicitly repeating. The code will try to lineup output by padding up to 40 spaces to the left, but since the cells keep expanding, that has to end somewhere.
<langsyntaxhighlight lang="perl">sub evolve {
my ($rule, $pattern) = @_;
my $offset = 0;
Line 777 ⟶ 938:
}
 
evolve(90, "010");</langsyntaxhighlight>
{{out}}
<pre>
Line 798 ⟶ 959:
=={{header|Phix}}==
Uses 0-expansion either side
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>string s = ".#.",
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
t=s, r = "........"
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">".#."</span><span style="color: #0000FF;">,</span>
integer rule = 18, k, l = length(s), w = 0
<span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"........"</span>
for i=1 to 8 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">rule</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">18</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
r[i] = iff(mod(rule,2)?'#':'.')
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
rule = floor(rule/2)
<span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">:</span><span style="color: #008000;">'.'</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #000000;">rule</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rule</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
for i=0 to 25 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
?repeat(' ',floor((55-length(s))/2))&s
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">25</span> <span style="color: #008080;">do</span>
for j=1 to l do
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">55</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))&</span><span style="color: #000000;">s</span>
k = (s[iff(j=1?l:j-1)]='#')*4
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
+ (s[ j ]='#')*2
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">l</span><span style="color: #0000FF;">:</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)]=</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">4</span>
+ (s[iff(j=l?1:j+1)]='#')+1
<span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">]=</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span>
t[j] = r[k]
<span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">l</span><span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)]=</span><span style="color: #008000;">'#'</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
end for
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
if t[1]='#' then t = '.'&t end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if t[$]='#' then t = t&'.' end if
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'#'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'.'</span><span style="color: #0000FF;">&</span><span style="color: #000000;">t</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
l = length(t)
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[$]=</span><span style="color: #008000;">'#'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">&</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
s = t
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
end for</lang>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 851 ⟶ 1,015:
Infinite generator but only print 25 lines of each rule.
 
<langsyntaxhighlight lang="python">def _notcell(c):
return '0' if c == '1' else '1'
 
Line 871 ⟶ 1,035:
print('\nRule: %i' % rule)
for i, c in zip(range(lines), eca_infinite('1', rule)):
print('%2i: %s%s' % (i, ' '*(lines - i), c.replace('0', '.').replace('1', '#')))</langsyntaxhighlight>
 
{{out}}
Line 932 ⟶ 1,096:
Uses solution to [[Elementary cellular automaton]] saved in file "Elementary_cellular_automata.rkt"
 
<langsyntaxhighlight lang="racket">#lang racket
; below is the code from the parent task
(require "Elementary_cellular_automata.rkt")
Line 964 ⟶ 1,128:
(show-automaton v #:step step #:push-right o)
(newline)
(ng/90/infinite v o)))</langsyntaxhighlight>
 
{{out}}
Line 1,017 ⟶ 1,181:
Based heavily on the code from the [[One-dimensional_cellular_automata#Raku|One-dimensional cellular automata]] task. Example uses rule 90 (Sierpinski triangle).
 
<syntaxhighlight lang="raku" perl6line>class Automaton {
has $.rule;
has @.cells;
Line 1,043 ⟶ 1,207:
 
# then calculate the other infinite number of rows, (may take a while)
$a++ for ^Inf;</langsyntaxhighlight>
{{out}}
<pre>░▲░
Line 1,070 ⟶ 1,234:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">def notcell(c)
c.tr('01','10')
end
Line 1,094 ⟶ 1,258:
end
end
end</langsyntaxhighlight>
The output is the same as the Python entry.
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func evolve(rule, bin) {
var offset = 0
var (l='', r='')
Line 1,112 ⟶ 1,276:
}
 
evolve(90, "010")</langsyntaxhighlight>
{{out}}
<pre>
Line 1,140 ⟶ 1,304:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create InfiniteElementaryAutomaton {
Line 1,195 ⟶ 1,359:
$rule run 25
$rule destroy
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,257 ⟶ 1,421:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var addNoCells = Fn.new { |s|
Line 1,295 ⟶ 1,459:
 
evolve.call(35, 90)
System.print()</langsyntaxhighlight>
 
{{out}}
Line 1,339 ⟶ 1,503:
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang="zkl">nLines,flipCell := 25, fcn(c){ (c=="1") and "0" or "1" };
foreach rule in (T(90,30)){
println("\nRule: ", rule);
Line 1,351 ⟶ 1,515:
C=[1..C.len()-2].pump(String,'wrap(n){ neighs2next[C[n-1,3]] });
}
}</langsyntaxhighlight>
{{out}}
<pre>
9,479

edits