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 / maze-solving.wiki

245 lines · 4981 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 tcorner "+"
10text thwall "---+"
11text thopen " +"
12text tvwall "|"
13text tcell " "
14text tpath " * "
15text tspace " "
16global seed ulong 1
17global ewall uchar 60
18global swall uchar 60
19global seen uchar 60
20global stk ulong 60
21global cand ulong 4
22global mark uchar 60
23text tlen "cells on the path from top left to bottom right: "
24global dist ulong 60
25global queue ulong 60
26note emit writes every byte of a to stdout; a short write is retried, an error exits 1
27fn emit a view uchar * to void
28o = ulong 0
29while lt o len a bound 4096
30k = write 1 a o sub len a o
31if le k 0
32exit 1
33end
34o = add o ulong k
35end
36end
37note emitu writes n in decimal; 20 digits hold every ulong
38fn emitu n ulong to void
39d = new uchar 20 0
40i = ulong 20
41v = n
42while true bound 20
43i = sub i 1
44put d i uchar add rem v 10 48
45v = div v 10
46if eq v 0
47break
48end
49end
50emit part d i sub 20 i
51end
52note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407
53note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33
54fn rnd to ulong
55x = add mul get seed 0 6364136223846793005 1442695040888963407
56put seed 0 x
57ret shr x 33
58end
59note the maze has 10 columns and 6 rows of cells, cell c = row*10 + column; ewall[c] and swall[c] are the
60note walls on its east and south sides, all standing at the start
61note neighbour says which cell lies in direction d (0 north, 1 east, 2 south, 3 west), 60 if none
62fn neighbour c ulong d ulong to ulong
63r = div c 10
64k = rem c 10
65if and eq d 0 gt r 0
66ret sub c 10
67end
68if and eq d 1 lt k 9
69ret add c 1
70end
71if and eq d 2 lt r 5
72ret add c 10
73end
74if and eq d 3 gt k 0
75ret sub c 1
76end
77ret 60
78end
79note knock removes the wall between neighbouring cells a and b
80fn knock a ulong b ulong to void
81if eq b add a 1
82put ewall a 0
83end
84if eq a add b 1
85put ewall b 0
86end
87if eq b add a 10
88put swall a 0
89end
90if eq a add b 10
91put swall b 0
92end
93end
94note recursive backtracker with an explicit stack: from the top cell, collect the unvisited neighbours in
95note the order north, east, south, west, pick one with rnd mod their count, knock through and push it;
96note with none left, pop; every pass pushes a new cell or pops, so 2*60 passes suffice
97fn generate to void
98for c ulong 60
99put ewall c 1
100put swall c 1
101put seen c 0
102end
103put seen 0 1
104put stk 0 0
105sp1 = ulong 1
106while gt sp1 0 bound 120
107c = get stk sub sp1 1
108k = ulong 0
109for d ulong 4
110b = neighbour c d
111if lt b 60
112if eq get seen b 0
113put cand k b
114k = add k 1
115end
116end
117end
118if eq k 0
119sp1 = sub sp1 1
120else
121b = get cand rem rnd k
122knock c b
123put seen b 1
124put stk sp1 b
125sp1 = add sp1 1
126end
127end
128end
129note draw prints the maze; cells with mark[c] = 1 show a *
130fn draw to void
131emit tcorner
132for k ulong 10
133emit thwall
134end
135emit nl
136for r ulong 6
137emit tvwall
138for k ulong 10
139c = add mul r 10 k
140if eq get mark c 1
141emit tpath
142else
143emit tcell
144end
145if eq get ewall c 1
146emit tvwall
147else
148emit tspace
149end
150end
151emit nl
152emit tcorner
153for k ulong 10
154if eq get swall add mul r 10 k 1
155emit thwall
156else
157emit thopen
158end
159end
160emit nl
161end
162end
163note opento says whether the wall between neighbouring cells a and b is open
164fn opento a ulong b ulong to bool
165if eq b add a 1
166ret eq get ewall a 0
167end
168if eq a add b 1
169ret eq get ewall b 0
170end
171if eq b add a 10
172ret eq get swall a 0
173end
174ret eq get swall b 0
175end
176note bfs labels every cell with its distance from the top-left cell (60 = not reached yet)
177fn bfs to void
178for c ulong 60
179put dist c 60
180end
181put dist 0 0
182put queue 0 0
183h = ulong 0
184t = ulong 1
185while lt h t bound 60
186c = get queue h
187h = add h 1
188for d ulong 4
189b = neighbour c d
190if lt b 60
191if and eq get dist b 60 opento c b
192put dist b add get dist c 1
193put queue t b
194t = add t 1
195end
196end
197end
198end
199end
200note walk back from the bottom-right cell, each step to the neighbour one closer to the start
201fn markpath to ulong
202c = ulong 59
203put mark c 1
204while ne c 0 bound 60
205for d ulong 4
206b = neighbour c d
207if lt b 60
208if and opento c b eq add get dist b 1 get dist c
209c = b
210break
211end
212end
213end
214put mark c 1
215end
216ret add get dist 59 1
217end
218fn main to int
219generate
220bfs
221n = markpath
222draw
223emit tlen
224emitu n
225emit nl
226ret 0
227end
228</syntaxhighlight>
229{{out}}
230<pre>
231+---+---+---+---+---+---+---+---+---+---+
232| * | | |
233+ +---+---+---+---+ + + +---+---+
234| * * * * | | | |
235+---+---+---+ + +---+ +---+---+ +
236| * * | * * | | |
237+ + + +---+---+---+---+---+ + +
238| * | * * | * * * * | | |
239+ +---+---+ +---+---+ + +---+---+
240| * * | * * | | * | |
241+---+ + +---+ +---+ +---+---+ +
242| * * | * * * * |
243+---+---+---+---+---+---+---+---+---+---+
244cells on the path from top left to bottom right: 27
245</pre>