interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / si / rosetta / tasks / spiral-matrix / spiral-matrix.om

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