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