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