interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / one-dimensional-cellular-automata.wiki

111 lines · 2188 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text tstart "_###_##_#_#_#_#__#__"
10text tgen "generation "
11text tcolon ": "
12global cur uchar 20
13global nxt uchar 20
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note emitb writes the single byte c
41fn emitb c ulong to void
42d = new uchar 1 0
43put d 0 uchar c
44emit d
45end
46note a cell is alive next generation when exactly two of itself and its two neighbours are alive
47note (cells beyond the ends are dead); the run stops when a generation repeats the one before
48fn cell i ulong to ulong
49if eq get cur i 35
50ret 1
51end
52ret 0
53end
54fn step to bool
55changed = false
56for i ulong 20
57n = cell i
58if gt i 0
59n = add n cell sub i 1
60end
61if lt i 19
62n = add n cell add i 1
63end
64v = uchar 95
65if eq n 2
66v = 35
67end
68if ne v get cur i
69changed = true
70end
71put nxt i v
72end
73for i ulong 20
74put cur i get nxt i
75end
76ret changed
77end
78note 20 cells can change at most 2^20 ways, but this start settles after 9 generations; 100 is ample
79fn main to int
80for i ulong 20
81put cur i get tstart i
82end
83g = ulong 0
84while true bound 100
85emit tgen
86emitu g
87emit tcolon
88for i ulong 20
89emitb ulong get cur i
90end
91emit nl
92if not step
93break
94end
95g = add g 1
96end
97ret 0
98end
99</syntaxhighlight>
100{{out}}
101<pre>
102generation 0: _###_##_#_#_#_#__#__
103generation 1: _#_#####_#_#_#______
104generation 2: __##___##_#_#_______
105generation 3: __##___###_#________
106generation 4: __##___#_##_________
107generation 5: __##____###_________
108generation 6: __##____#_#_________
109generation 7: __##_____#__________
110generation 8: __##________________
111</pre>