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

147 lines · 2693 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 "-"
9text thead "n most swops\n"
10global perm ulong 10
11global deck ulong 10
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitu writes n in decimal; 20 digits hold every ulong
24fn emitu n ulong to void
25d = new uchar 20 0
26i = ulong 20
27v = n
28while true bound 20
29i = sub i 1
30put d i uchar add rem v 10 48
31v = div v 10
32if eq v 0
33break
34end
35end
36emit part d i sub 20 i
37end
38note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut)
39fn emitw n ulong w ulong to void
40d = new uchar 20 32
41i = ulong 20
42v = n
43while true bound 20
44i = sub i 1
45put d i uchar add rem v 10 48
46v = div v 10
47if eq v 0
48break
49end
50end
51if gt w sub 20 i
52i = sub 20 w
53end
54emit part d i sub 20 i
55end
56note nextp: the next permutation in lexicographic order, false after the last
57fn nextp n ulong to bool
58if lt n 2
59ret false
60end
61i = sub n 1
62while gt i 0 bound 10
63if lt get perm sub i 1 get perm i
64break
65end
66i = sub i 1
67end
68if eq i 0
69ret false
70end
71j = sub n 1
72while le get perm j get perm sub i 1 bound 10
73j = sub j 1
74end
75t = get perm sub i 1
76put perm sub i 1 get perm j
77put perm j t
78k = sub n 1
79while lt i k bound 10
80t2 = get perm i
81put perm i get perm k
82put perm k t2
83i = add i 1
84k = sub k 1
85end
86ret true
87end
88note swops plays the game on a copy of perm (cards 1..n): while the top card is m > 1, reverse the top
89note m cards; it ends because the deck's lexicographic order rises with each reversal, and the known
90note maxima for n <= 9 are at most 30, so 1000 rounds are plenty
91fn swops n ulong to ulong
92for i ulong n
93put deck i get perm i
94end
95c = ulong 0
96while ne get deck 0 1 bound 1000
97m = get deck 0
98for x ulong shr m 1
99t = get deck x
100put deck x get deck sub sub m x 1
101put deck sub sub m x 1 t
102end
103c = add c 1
104end
105ret c
106end
107note most tries every one of the n! decks (9! = 362880 at most)
108fn most n ulong to ulong
109for i ulong n
110put perm i add i 1
111end
112b = ulong 0
113while true bound 362880
114s = swops n
115if gt s b
116b = s
117end
118if not nextp n
119break
120end
121end
122ret b
123end
124fn main to int
125emit thead
126for x ulong 9
127n = add x 1
128emitu n
129emitw most n 12
130emit nl
131end
132ret 0
133end
134</syntaxhighlight>
135{{out}}
136<pre>
137n most swops
1381 0
1392 1
1403 2
1414 4
1425 7
1436 10
1447 16
1458 22
1469 30
147</pre>