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-mv/blob/main/si/rosetta/tasks/knights-tour/knights-tour.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / si / rosetta / tasks / knights-tour / knights-tour.om` 134 lines ยท 2415 bytes Raw Blame History 1 note Rosetta Code task "Knight's tour": https://rosettacode.org/wiki/Knight's_tour 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 tstart "Warnsdorff tour from a1; each square shows its move number, rank 8 at the top\n" 8 global board ulong 64 9 global mdx long 8 10 global mdy long 8 11 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 12 fn emit a view uchar * to void 13 o = ulong 0 14 while lt o len a bound 4096 15 k = write 1 a o sub len a o 16 if le k 0 17 exit 1 18 end 19 o = add o ulong k 20 end 21 end 22 note emitu writes n in decimal; 20 digits hold every ulong 23 fn emitu n ulong to void 24 d = new uchar 20 0 25 i = ulong 20 26 v = n 27 while true bound 20 28 i = sub i 1 29 put d i uchar add rem v 10 48 30 v = div v 10 31 if eq v 0 32 break 33 end 34 end 35 emit part d i sub 20 i 36 end 37 note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) 38 fn emitw n ulong w ulong to void 39 d = new uchar 20 32 40 i = ulong 20 41 v = n 42 while true bound 20 43 i = sub i 1 44 put d i uchar add rem v 10 48 45 v = div v 10 46 if eq v 0 47 break 48 end 49 end 50 if gt w sub 20 i 51 i = sub 20 w 52 end 53 emit part d i sub 20 i 54 end 55 note target is the square a knight reaches from sq with move m, or 64 when it leaves the board 56 fn target sq ulong m ulong to ulong 57 f = add long rem sq 8 get mdx m 58 r = add long div sq 8 get mdy m 59 if or or lt f 0 gt f 7 or lt r 0 gt r 7 60 ret 64 61 end 62 ret ulong add mul r 8 f 63 end 64 note degree counts the unvisited squares reachable from sq 65 fn degree sq ulong to ulong 66 d = ulong 0 67 for m ulong 8 68 t = target sq m 69 if lt t 64 70 if eq get board t 0 71 d = add d 1 72 end 73 end 74 end 75 ret d 76 end 77 note Warnsdorff's rule: always move to the unvisited square with the fewest onward moves; ties go to 78 note the first in the move order (2,1) (1,2) (-1,2) (-2,1) (-2,-1) (-1,-2) (1,-2) (2,-1) 79 fn tour to bool 80 sq = ulong 0 81 put board 0 1 82 for k ulong 63 83 b = ulong 64 84 bd = ulong 9 85 for m ulong 8 86 t = target sq m 87 if lt t 64 88 if eq get board t 0 89 d = degree t 90 if lt d bd 91 b = t 92 bd = d 93 end 94 end 95 end 96 end 97 if eq b 64 98 ret false 99 end 100 put board b add k 2 101 sq = b 102 end 103 ret true 104 end 105 fn main to int 106 put mdx 0 2 107 put mdy 0 1 108 put mdx 1 1 109 put mdy 1 2 110 put mdx 2 -1 111 put mdy 2 2 112 put mdx 3 -2 113 put mdy 3 1 114 put mdx 4 -2 115 put mdy 4 -1 116 put mdx 5 -1 117 put mdy 5 -2 118 put mdx 6 1 119 put mdy 6 -2 120 put mdx 7 2 121 put mdy 7 -1 122 if not tour 123 exit 1 124 end 125 emit tstart 126 for x ulong 8 127 r = sub 7 x 128 for f ulong 8 129 emitw get board add mul r 8 f 3 130 end 131 emit nl 132 end 133 ret 0 134 end