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-mv/blob/main/si/rosetta/tasks/spiral-matrix/spiral-matrix.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / spiral-matrix / spiral-matrix.om` 99 lines ยท 1891 bytes Raw Blame History 1 note Rosetta Code task "Spiral matrix": https://rosettacode.org/wiki/Spiral_matrix 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 global grid ulong 64 8 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 9 fn emit a view uchar * to void 10 o = ulong 0 11 while lt o len a bound 4096 12 k = write 1 a o sub len a o 13 if le k 0 14 exit 1 15 end 16 o = add o ulong k 17 end 18 end 19 note emitu writes n in decimal; 20 digits hold every ulong 20 fn emitu n ulong to void 21 d = new uchar 20 0 22 i = ulong 20 23 v = n 24 while true bound 20 25 i = sub i 1 26 put d i uchar add rem v 10 48 27 v = div v 10 28 if eq v 0 29 break 30 end 31 end 32 emit part d i sub 20 i 33 end 34 note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) 35 fn emitw n ulong w ulong to void 36 d = new uchar 20 32 37 i = ulong 20 38 v = n 39 while true bound 20 40 i = sub i 1 41 put d i uchar add rem v 10 48 42 v = div v 10 43 if eq v 0 44 break 45 end 46 end 47 if gt w sub 20 i 48 i = sub 20 w 49 end 50 emit part d i sub 20 i 51 end 52 note the walk starts at the top left going right and turns clockwise whenever the next cell is off the 53 note grid or already numbered; grid holds number+1 so that 0 means empty 54 fn spiral n ulong to void 55 for i ulong mul n n 56 put grid i 0 57 end 58 r = long 0 59 c = long 0 60 dr = long 0 61 dc = long 1 62 for k ulong mul n n 63 put grid add mul ulong r n ulong c add k 1 64 nr = add r dr 65 nc = add c dc 66 if blocked nr nc n 67 t = dr 68 dr = dc 69 dc = neg t 70 nr = add r dr 71 nc = add c dc 72 end 73 r = nr 74 c = nc 75 end 76 end 77 note blocked says whether (r, c) is outside the n x n grid or already numbered 78 fn blocked r long c long n ulong to bool 79 if or or lt r 0 ge r long n or lt c 0 ge c long n 80 ret true 81 end 82 ret ne get grid add mul ulong r n ulong c 0 83 end 84 fn show n ulong to void 85 for i ulong n 86 for j ulong n 87 emitw sub get grid add mul i n j 1 3 88 end 89 emit nl 90 end 91 end 92 fn main to int 93 spiral 5 94 show 5 95 emit nl 96 spiral 8 97 show 8 98 ret 0 99 end