interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / langtons-ant.wiki

170 lines · 4454 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text tsteps "steps until the ant leaves the grid: "
10text tbox "black cells, rows and columns "
11text tto ".."
12text tdot "."
13text thash "#"
14global grid uchar 10000
15global box ulong 4
16note emit writes every byte of a to stdout; a short write is retried, an error exits 1
17fn emit a view uchar * to void
18o = ulong 0
19while lt o len a bound 4096
20k = write 1 a o sub len a o
21if le k 0
22exit 1
23end
24o = add o ulong k
25end
26end
27note emitu writes n in decimal; 20 digits hold every ulong
28fn emitu n ulong to void
29d = new uchar 20 0
30i = ulong 20
31v = n
32while true bound 20
33i = sub i 1
34put d i uchar add rem v 10 48
35v = div v 10
36if eq v 0
37break
38end
39end
40emit part d i sub 20 i
41end
42note the ant starts at (50, 50) of a white 100 x 100 grid facing up; on white it turns right, on black
43note left, then flips the cell and steps forward; directions 0..3 are up, right, down, left
44fn walk to ulong
45r = long 50
46c = long 50
47d = ulong 0
48s = ulong 0
49note the ant is known to leave a grid this size after about 12000 steps (the highway starts near
50note step 10000), so 999999 is a safe bound
51while and and ge r 0 lt r 100 and ge c 0 lt c 100 bound 999999
52k = add mul ulong r 100 ulong c
53if eq get grid k 0
54d = rem add d 1 4
55put grid k 1
56else
57d = rem add d 3 4
58put grid k 0
59end
60r = add r dy d
61c = add c dx d
62s = add s 1
63end
64ret s
65end
66fn dy d ulong to long
67if eq d 0
68ret -1
69end
70if eq d 2
71ret 1
72end
73ret 0
74end
75fn dx d ulong to long
76if eq d 1
77ret 1
78end
79if eq d 3
80ret -1
81end
82ret 0
83end
84note bounds finds the smallest box holding every black cell: box = top, bottom, left, right
85fn bounds to void
86put box 0 99
87put box 1 0
88put box 2 99
89put box 3 0
90for r ulong 100
91for c ulong 100
92if eq get grid add mul r 100 c 1
93grow r c
94end
95end
96end
97end
98fn grow r ulong c ulong to void
99if lt r get box 0
100put box 0 r
101end
102if gt r get box 1
103put box 1 r
104end
105if lt c get box 2
106put box 2 c
107end
108if gt c get box 3
109put box 3 c
110end
111end
112note row prints grid row r between the box's left and right columns
113fn row r ulong to void
114for x ulong add sub get box 3 get box 2 1
115c = add x get box 2
116if eq get grid add mul r 100 c 1
117emit thash
118else
119emit tdot
120end
121end
122emit nl
123end
124fn main to int
125emit tsteps
126emitu walk
127emit nl
128bounds
129emit tbox
130emitu get box 0
131emit tto
132emitu get box 1
133emit sp
134emitu get box 2
135emit tto
136emitu get box 3
137emit nl
138for r ulong add sub get box 1 get box 0 1
139row add r get box 0
140end
141ret 0
142end
143</syntaxhighlight>
144{{out}}
145<pre>
146steps until the ant leaves the grid: 11669
147black cells, rows and columns 28..78 0..79
148..........................................##..############..##..................
149.........................................#..####..........#..##.................
150........................................###...##............##.#................
151........................................#.#..#.........#..#....#................
152....................................##..##.#.#.........###.......#..............
153.................................###.#..#...#.....#.....##.##..###..............
154..................................#.#..###..##.####.##...#.#..#.##..##..........
155..................................#.###.##..#.##..###.#.#.....###...###.........
156................................#.....#...#####.#.#..####..#...###.#.#.#........
157...............................###.##...#.####..##.##.######.#.###.#...#........
158...............................#.###.#.##.#.#.##.##.##.#...#####.###.##.........
159...................................#.#...#.##.###...#...#.#..####....#.##.......
160................................#..#.........##.##...#..##.....##.#.....##......
161...
162.###...#..##........................................................##......##..
163#..###..#.#..........................................................##.........
164.###.#..##......................................................................
165#.#.#.#.#.......................................................................
166.####.##........................................................................
167.#.##.#.........................................................................
168..####..........................................................................
169...##...........................................................................
170</pre>