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

102 lines · 1878 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 number the cells along the anti-diagonals d = i + j; even diagonals run up and to the right
55note (i falling), odd ones down and to the left (i rising)
56fn zigzag n ulong to void
57k = ulong 0
58for d ulong sub mul n 2 1
59for x ulong add d 1
60i = x
61if eq rem d 2 0
62i = sub d x
63end
64if and lt i n le sub d i sub n 1
65put grid add mul i n sub d i k
66k = add k 1
67end
68end
69end
70end
71fn show n ulong to void
72for i ulong n
73for j ulong n
74emitw get grid add mul i n j 3
75end
76emit nl
77end
78end
79fn main to int
80zigzag 5
81show 5
82emit nl
83zigzag 6
84show 6
85ret 0
86end
87</syntaxhighlight>
88{{out}}
89<pre>
90 0 1 5 6 14
91 2 4 7 13 15
92 3 8 12 16 21
93 9 11 17 20 22
94 10 18 19 23 24
95
96 0 1 5 6 14 15
97 2 4 7 13 16 25
98 3 8 12 17 24 26
99 9 11 18 23 27 32
100 10 19 22 28 31 33
101 20 21 29 30 34 35
102</pre>