interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / one-dimensional-cellular-automata / one-dimensional-cellular-automata.om

96 lines · 1779 bytes Raw Blame History
1note Rosetta Code task "One-dimensional cellular automata": https://rosettacode.org/wiki/One-dimensional_cellular_automata
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tstart "_###_##_#_#_#_#__#__"
8text tgen "generation "
9text tcolon ": "
10global cur uchar 20
11global nxt uchar 20
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note emitb writes the single byte c
39fn emitb c ulong to void
40d = new uchar 1 0
41put d 0 uchar c
42emit d
43end
44note a cell is alive next generation when exactly two of itself and its two neighbours are alive
45note (cells beyond the ends are dead); the run stops when a generation repeats the one before
46fn cell i ulong to ulong
47if eq get cur i 35
48ret 1
49end
50ret 0
51end
52fn step to bool
53changed = false
54for i ulong 20
55n = cell i
56if gt i 0
57n = add n cell sub i 1
58end
59if lt i 19
60n = add n cell add i 1
61end
62v = uchar 95
63if eq n 2
64v = 35
65end
66if ne v get cur i
67changed = true
68end
69put nxt i v
70end
71for i ulong 20
72put cur i get nxt i
73end
74ret changed
75end
76note 20 cells can change at most 2^20 ways, but this start settles after 9 generations; 100 is ample
77fn main to int
78for i ulong 20
79put cur i get tstart i
80end
81g = ulong 0
82while true bound 100
83emit tgen
84emitu g
85emit tcolon
86for i ulong 20
87emitb ulong get cur i
88end
89emit nl
90if not step
91break
92end
93g = add g 1
94end
95ret 0
96end