Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/topswops/topswops.om # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / topswops / topswops.om` 131 lines ยท 2409 bytes Raw Blame History 1 note Rosetta Code task "Topswops": https://rosettacode.org/wiki/Topswops 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text thead "n most swops\n" 8 global perm ulong 10 9 global deck ulong 10 10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 11 fn emit a view uchar * to void 12 o = ulong 0 13 while lt o len a bound 4096 14 k = write 1 a o sub len a o 15 if le k 0 16 exit 1 17 end 18 o = add o ulong k 19 end 20 end 21 note emitu writes n in decimal; 20 digits hold every ulong 22 fn emitu n ulong to void 23 d = new uchar 20 0 24 i = ulong 20 25 v = n 26 while true bound 20 27 i = sub i 1 28 put d i uchar add rem v 10 48 29 v = div v 10 30 if eq v 0 31 break 32 end 33 end 34 emit part d i sub 20 i 35 end 36 note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) 37 fn emitw n ulong w ulong to void 38 d = new uchar 20 32 39 i = ulong 20 40 v = n 41 while true bound 20 42 i = sub i 1 43 put d i uchar add rem v 10 48 44 v = div v 10 45 if eq v 0 46 break 47 end 48 end 49 if gt w sub 20 i 50 i = sub 20 w 51 end 52 emit part d i sub 20 i 53 end 54 note nextp: the next permutation in lexicographic order, false after the last 55 fn nextp n ulong to bool 56 if lt n 2 57 ret false 58 end 59 i = sub n 1 60 while gt i 0 bound 10 61 if lt get perm sub i 1 get perm i 62 break 63 end 64 i = sub i 1 65 end 66 if eq i 0 67 ret false 68 end 69 j = sub n 1 70 while le get perm j get perm sub i 1 bound 10 71 j = sub j 1 72 end 73 t = get perm sub i 1 74 put perm sub i 1 get perm j 75 put perm j t 76 k = sub n 1 77 while lt i k bound 10 78 t2 = get perm i 79 put perm i get perm k 80 put perm k t2 81 i = add i 1 82 k = sub k 1 83 end 84 ret true 85 end 86 note swops plays the game on a copy of perm (cards 1..n): while the top card is m > 1, reverse the top 87 note m cards; it ends because the deck's lexicographic order rises with each reversal, and the known 88 note maxima for n <= 9 are at most 30, so 1000 rounds are plenty 89 fn swops n ulong to ulong 90 for i ulong n 91 put deck i get perm i 92 end 93 c = ulong 0 94 while ne get deck 0 1 bound 1000 95 m = get deck 0 96 for x ulong shr m 1 97 t = get deck x 98 put deck x get deck sub sub m x 1 99 put deck sub sub m x 1 t 100 end 101 c = add c 1 102 end 103 ret c 104 end 105 note most tries every one of the n! decks (9! = 362880 at most) 106 fn most n ulong to ulong 107 for i ulong n 108 put perm i add i 1 109 end 110 b = ulong 0 111 while true bound 362880 112 s = swops n 113 if gt s b 114 b = s 115 end 116 if not nextp n 117 break 118 end 119 end 120 ret b 121 end 122 fn main to int 123 emit thead 124 for x ulong 9 125 n = add x 1 126 emitu n 127 emitw most n 12 128 emit nl 129 end 130 ret 0 131 end