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