Elementary cellular automaton/Infinite length: Difference between revisions
Elementary cellular automaton/Infinite length (view source)
Revision as of 10:11, 27 August 2022
, 1 year agosyntax highlighting fixup automation
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 18:
{{trans|Nim}}
<
V result = ‘’
L(i) 0 .< cells.len - 2
Line 45:
cells = step(cells, rule)
evolve(35, 90)</
{{out}}
Line 88:
=={{header|C++}}==
<
#include <iostream>
#include <iomanip>
Line 130:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 163:
=={{header|D}}==
{{trans|Python}}
<
std.algorithm;
alias R = replicate;
Line 187:
}
}
}</
The output is the same as the Python entry.
Line 193:
{{works with|Elixir|1.3}}
{{trans|Ruby}}
<
defmodule Elementary_cellular_automaton do
def infinite(cell, rule, times) do
Line 224:
IO.puts "\nRule : #{rule}"
Elementary_cellular_automaton.infinite("1", rule, 25)
end)</
{{out}}
Line 285:
=={{header|Go}}==
{{trans|C++}}
<
import (
Line 346:
fmt.Println()
}
}</
{{out}}
Line 412:
First we provide the datatype, the viewer and constructor:
<
import Control.Comonad
Line 424:
fromList [] = fromList [0]
fromList (x:xs) = let zeros = Inf.repeat 0
in Cells zeros x (xs +++ zeros)</
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:
<
extract (Cells _ x _) = x
duplicate x = Cells (rewind left x) x (rewind right x)
Line 437:
runCA rule = iterate (=>> step)
where step (Cells (l ::: _) x (r ::: _)) = rule l x r</
Following is the rule definition and I/O routine:
<
displayCA n w rule init = mapM_ putStrLn $ take n result
where result = fmap display . view w <$> runCA rule init
display 0 = ' '
display _ = '*'</
{{Out}}
Line 494:
Implementation:
<
trim=: |.@(}.~ ] i. 1-{.)^:2
next=: trim@(((8$2) #: [) {~ 2 #. 1 - [: |: |.~"1 0&_1 0 1@]) ext9</
In other words, a wrapped version of the [[Elementary_cellular_automaton#J|original implementation]].
Line 502:
example use:
<
*
* *
Line 511:
* * * *
* * * * * * * *
* *</
Looks like a [[Sierpinski_triangle|Sierpinski triangle]]
Line 521:
'''Preliminaries'''
<syntaxhighlight lang="jq">
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
Line 546:
# input: an array, e.g. as produced by [7|stream]
# output: the corresponding binary string
def to_b: reverse | map(tostring) | join("") | sub("^0";"");</
'''The Cellular Automaton'''
<
# giving the rows produced by the eca defined by
# $cells (a string) and $rule (an integer)
Line 566:
| .c = ([range(1; .c|length - 1) as $i | $neighbours2next[.c[$i-1:$i+2] ]] | join(""));
[$i, .c] ) ;
</syntaxhighlight>
'''The Task'''
<
def main($lines):
(90, 30) as $rule
Line 576:
| ($line|lpad(3)) + " " * ($lines - $line) + (.[1] | tr("01"; ".#") )));
main(25)</
{{out}}
As for [[#Python|Python]].
=={{header|Julia}}==
{{trans|Python}}
<
notcell(cell) = (cell == '1') ? '0' : '1'
rulebits = reverse(string(rule, base = 2, pad = 8))
Line 605:
testinfcells(25)
</
<pre>
Rule: 90 (01011010)
Line 664:
=={{header|Kotlin}}==
{{trans|C++}}
<
fun evolve(l: Int, rule: Int) {
Line 704:
evolve(35, 90)
println()
}</
{{out}}
Line 748:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
An infinite background is built-in the language:
<
CellularAutomaton[30, {{1}, 0}, 15] // ArrayPlot</
=={{header|Nim}}==
{{trans|Kotlin}}
<
Line 786:
evolve(35, 90)</
{{out}}
Line 828:
=={{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.
<
my ($rule, $pattern) = @_;
my $offset = 0;
Line 849:
}
evolve(90, "010");</
{{out}}
<pre>
Line 870:
=={{header|Phix}}==
Uses 0-expansion either side
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 892:
<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>
<!--</
{{out}}
<pre>
Line 926:
Infinite generator but only print 25 lines of each rule.
<
return '0' if c == '1' else '1'
Line 946:
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', '#')))</
{{out}}
Line 1,007:
Uses solution to [[Elementary cellular automaton]] saved in file "Elementary_cellular_automata.rkt"
<
; below is the code from the parent task
(require "Elementary_cellular_automata.rkt")
Line 1,039:
(show-automaton v #:step step #:push-right o)
(newline)
(ng/90/infinite v o)))</
{{out}}
Line 1,092:
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"
has $.rule;
has @.cells;
Line 1,118:
# then calculate the other infinite number of rows, (may take a while)
$a++ for ^Inf;</
{{out}}
<pre>░▲░
Line 1,145:
=={{header|Ruby}}==
{{trans|Python}}
<
c.tr('01','10')
end
Line 1,169:
end
end
end</
The output is the same as the Python entry.
=={{header|Sidef}}==
{{trans|Perl}}
<
var offset = 0
var (l='', r='')
Line 1,187:
}
evolve(90, "010")</
{{out}}
<pre>
Line 1,215:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
oo::class create InfiniteElementaryAutomaton {
Line 1,270:
$rule run 25
$rule destroy
}</
{{out}}
<pre>
Line 1,332:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
var addNoCells = Fn.new { |s|
Line 1,370:
evolve.call(35, 90)
System.print()</
{{out}}
Line 1,414:
=={{header|zkl}}==
{{trans|D}}
<
foreach rule in (T(90,30)){
println("\nRule: ", rule);
Line 1,426:
C=[1..C.len()-2].pump(String,'wrap(n){ neighs2next[C[n-1,3]] });
}
}</
{{out}}
<pre>
|