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

231 +---+---+---+---+---+---+---+---+---+---+

232 | * | | |

233 + +---+---+---+---+ + + +---+---+

234 | * * * * | | | |

235 +---+---+---+ + +---+ +---+---+ +

236 | * * | * * | | |

237 + + + +---+---+---+---+---+ + +

238 | * | * * | * * * * | | |

239 + +---+---+ +---+---+ + +---+---+

240 | * * | * * | | * | |

241 +---+ + +---+ +---+ +---+---+ +

242 | * * | * * * * |

243 +---+---+---+---+---+---+---+---+---+---+

244 cells on the path from top left to bottom right: 27

245