interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / maze-solving / maze-solving.om

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