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 / topswops / topswops.om

131 lines · 2409 bytes Raw Blame History
1note Rosetta Code task "Topswops": https://rosettacode.org/wiki/Topswops
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text thead "n most swops\n"
8global perm ulong 10
9global deck ulong 10
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 nextp: the next permutation in lexicographic order, false after the last
55fn nextp n ulong to bool
56if lt n 2
57ret false
58end
59i = sub n 1
60while gt i 0 bound 10
61if lt get perm sub i 1 get perm i
62break
63end
64i = sub i 1
65end
66if eq i 0
67ret false
68end
69j = sub n 1
70while le get perm j get perm sub i 1 bound 10
71j = sub j 1
72end
73t = get perm sub i 1
74put perm sub i 1 get perm j
75put perm j t
76k = sub n 1
77while lt i k bound 10
78t2 = get perm i
79put perm i get perm k
80put perm k t2
81i = add i 1
82k = sub k 1
83end
84ret true
85end
86note swops plays the game on a copy of perm (cards 1..n): while the top card is m > 1, reverse the top
87note m cards; it ends because the deck's lexicographic order rises with each reversal, and the known
88note maxima for n <= 9 are at most 30, so 1000 rounds are plenty
89fn swops n ulong to ulong
90for i ulong n
91put deck i get perm i
92end
93c = ulong 0
94while ne get deck 0 1 bound 1000
95m = get deck 0
96for x ulong shr m 1
97t = get deck x
98put deck x get deck sub sub m x 1
99put deck sub sub m x 1 t
100end
101c = add c 1
102end
103ret c
104end
105note most tries every one of the n! decks (9! = 362880 at most)
106fn most n ulong to ulong
107for i ulong n
108put perm i add i 1
109end
110b = ulong 0
111while true bound 362880
112s = swops n
113if gt s b
114b = s
115end
116if not nextp n
117break
118end
119end
120ret b
121end
122fn main to int
123emit thead
124for x ulong 9
125n = add x 1
126emitu n
127emitw most n 12
128emit nl
129end
130ret 0
131end