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 / spiral-matrix.wiki

119 lines · 2306 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 "-"
9global grid ulong 64
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut)
37fn emitw n ulong w ulong to void
38d = new uchar 20 32
39i = ulong 20
40v = n
41while true bound 20
42i = sub i 1
43put d i uchar add rem v 10 48
44v = div v 10
45if eq v 0
46break
47end
48end
49if gt w sub 20 i
50i = sub 20 w
51end
52emit part d i sub 20 i
53end
54note the walk starts at the top left going right and turns clockwise whenever the next cell is off the
55note grid or already numbered; grid holds number+1 so that 0 means empty
56fn spiral n ulong to void
57for i ulong mul n n
58put grid i 0
59end
60r = long 0
61c = long 0
62dr = long 0
63dc = long 1
64for k ulong mul n n
65put grid add mul ulong r n ulong c add k 1
66nr = add r dr
67nc = add c dc
68if blocked nr nc n
69t = dr
70dr = dc
71dc = neg t
72nr = add r dr
73nc = add c dc
74end
75r = nr
76c = nc
77end
78end
79note blocked says whether (r, c) is outside the n x n grid or already numbered
80fn blocked r long c long n ulong to bool
81if or or lt r 0 ge r long n or lt c 0 ge c long n
82ret true
83end
84ret ne get grid add mul ulong r n ulong c 0
85end
86fn show n ulong to void
87for i ulong n
88for j ulong n
89emitw sub get grid add mul i n j 1 3
90end
91emit nl
92end
93end
94fn main to int
95spiral 5
96show 5
97emit nl
98spiral 8
99show 8
100ret 0
101end
102</syntaxhighlight>
103{{out}}
104<pre>
105 0 1 2 3 4
106 15 16 17 18 5
107 14 23 24 19 6
108 13 22 21 20 7
109 12 11 10 9 8
110
111 0 1 2 3 4 5 6 7
112 27 28 29 30 31 32 33 8
113 26 47 48 49 50 51 34 9
114 25 46 59 60 61 52 35 10
115 24 45 58 63 62 53 36 11
116 23 44 57 56 55 54 37 12
117 22 43 42 41 40 39 38 13
118 21 20 19 18 17 16 15 14
119</pre>