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