interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / langtons-ant / langtons-ant.om

140 lines · 2529 bytes Raw Blame History
1note Rosetta Code task "Langton's ant": https://rosettacode.org/wiki/Langton's_ant
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text tsteps "steps until the ant leaves the grid: "
8text tbox "black cells, rows and columns "
9text tto ".."
10text tdot "."
11text thash "#"
12global grid uchar 10000
13global box ulong 4
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note the ant starts at (50, 50) of a white 100 x 100 grid facing up; on white it turns right, on black
41note left, then flips the cell and steps forward; directions 0..3 are up, right, down, left
42fn walk to ulong
43r = long 50
44c = long 50
45d = ulong 0
46s = ulong 0
47note the ant is known to leave a grid this size after about 12000 steps (the highway starts near
48note step 10000), so 999999 is a safe bound
49while and and ge r 0 lt r 100 and ge c 0 lt c 100 bound 999999
50k = add mul ulong r 100 ulong c
51if eq get grid k 0
52d = rem add d 1 4
53put grid k 1
54else
55d = rem add d 3 4
56put grid k 0
57end
58r = add r dy d
59c = add c dx d
60s = add s 1
61end
62ret s
63end
64fn dy d ulong to long
65if eq d 0
66ret -1
67end
68if eq d 2
69ret 1
70end
71ret 0
72end
73fn dx d ulong to long
74if eq d 1
75ret 1
76end
77if eq d 3
78ret -1
79end
80ret 0
81end
82note bounds finds the smallest box holding every black cell: box = top, bottom, left, right
83fn bounds to void
84put box 0 99
85put box 1 0
86put box 2 99
87put box 3 0
88for r ulong 100
89for c ulong 100
90if eq get grid add mul r 100 c 1
91grow r c
92end
93end
94end
95end
96fn grow r ulong c ulong to void
97if lt r get box 0
98put box 0 r
99end
100if gt r get box 1
101put box 1 r
102end
103if lt c get box 2
104put box 2 c
105end
106if gt c get box 3
107put box 3 c
108end
109end
110note row prints grid row r between the box's left and right columns
111fn row r ulong to void
112for x ulong add sub get box 3 get box 2 1
113c = add x get box 2
114if eq get grid add mul r 100 c 1
115emit thash
116else
117emit tdot
118end
119end
120emit nl
121end
122fn main to int
123emit tsteps
124emitu walk
125emit nl
126bounds
127emit tbox
128emitu get box 0
129emit tto
130emitu get box 1
131emit sp
132emitu get box 2
133emit tto
134emitu get box 3
135emit nl
136for r ulong add sub get box 1 get box 0 1
137row add r get box 0
138end
139ret 0
140end