omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / wireworld / wireworld.om
| 1 | note Rosetta Code task "Wireworld": https://rosettacode.org/wiki/Wireworld |
| 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 "tH.........|. . | ... |. . |Ht.. ......" |
| 8 | text tgen "generation " |
| 9 | global cur uchar 55 |
| 10 | global nxt uchar 55 |
| 11 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 12 | fn emit a view uchar * to void |
| 13 | o = ulong 0 |
| 14 | while lt o len a bound 4096 |
| 15 | k = write 1 a o sub len a o |
| 16 | if le k 0 |
| 17 | exit 1 |
| 18 | end |
| 19 | o = add o ulong k |
| 20 | end |
| 21 | end |
| 22 | note emitu writes n in decimal; 20 digits hold every ulong |
| 23 | fn emitu n ulong to void |
| 24 | d = new uchar 20 0 |
| 25 | i = ulong 20 |
| 26 | v = n |
| 27 | while true bound 20 |
| 28 | i = sub i 1 |
| 29 | put d i uchar add rem v 10 48 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | emit part d i sub 20 i |
| 36 | end |
| 37 | note emitb writes the single byte c |
| 38 | fn emitb c ulong to void |
| 39 | d = new uchar 1 0 |
| 40 | put d 0 uchar c |
| 41 | emit d |
| 42 | end |
| 43 | note the 5 x 11 board from the task: H electron head, t tail, . conductor, space empty; a head becomes a |
| 44 | note tail, a tail a conductor, and a conductor a head when 1 or 2 of its 8 neighbours are heads |
| 45 | fn heads r long c long to ulong |
| 46 | n = ulong 0 |
| 47 | for a ulong 3 |
| 48 | for b ulong 3 |
| 49 | y = sub add r long a 1 |
| 50 | x = sub add c long b 1 |
| 51 | if and and ge y 0 lt y 5 and ge x 0 lt x 11 |
| 52 | if eq get cur add mul ulong y 11 ulong x 72 |
| 53 | n = add n 1 |
| 54 | end |
| 55 | end |
| 56 | end |
| 57 | end |
| 58 | ret n |
| 59 | end |
| 60 | fn rule k ulong n ulong to uchar |
| 61 | v = get cur k |
| 62 | if eq v 72 |
| 63 | ret 116 |
| 64 | end |
| 65 | if eq v 116 |
| 66 | ret 46 |
| 67 | end |
| 68 | if and eq v 46 or eq n 1 eq n 2 |
| 69 | ret 72 |
| 70 | end |
| 71 | ret v |
| 72 | end |
| 73 | fn step to void |
| 74 | for r ulong 5 |
| 75 | for c ulong 11 |
| 76 | k = add mul r 11 c |
| 77 | put nxt k rule k heads long r long c |
| 78 | end |
| 79 | end |
| 80 | for i ulong 55 |
| 81 | put cur i get nxt i |
| 82 | end |
| 83 | end |
| 84 | fn main to int |
| 85 | for r ulong 5 |
| 86 | for c ulong 11 |
| 87 | put cur add mul r 11 c get tstart add mul r 12 c |
| 88 | end |
| 89 | end |
| 90 | for g ulong 10 |
| 91 | emit tgen |
| 92 | emitu g |
| 93 | emit nl |
| 94 | for i ulong 55 |
| 95 | emitb ulong get cur i |
| 96 | if eq rem i 11 10 |
| 97 | emit nl |
| 98 | end |
| 99 | end |
| 100 | step |
| 101 | end |
| 102 | ret 0 |
| 103 | end |