interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / wireworld / wireworld.om

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