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/sudoku/sudoku.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 / sudoku / sudoku.om` 110 lines ยท 1973 bytes Raw Blame History 1 note Rosetta Code task "Sudoku": https://rosettacode.org/wiki/Sudoku 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 puzzle "`850002400720000009004000000000107002305000900040000000000080070017000000000036040`" 8 text tbar "------+-------+------\n" 9 text tsep "| " 10 text tsolved "solved:\n" 11 global grid ulong 81 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 ok says whether digit d may go in cell c: not already in its row, column or 3x3 box 39 fn ok c ulong d ulong to bool 40 r = div c 9 41 k = rem c 9 42 b = add mul mul div r 3 3 9 mul div k 3 3 43 for i ulong 9 44 if eq get grid add mul r 9 i d 45 ret false 46 end 47 if eq get grid add mul i 9 k d 48 ret false 49 end 50 q = add b add mul div i 3 9 rem i 3 51 if eq get grid q d 52 ret false 53 end 54 end 55 ret true 56 end 57 note solve fills the first empty cell at or after c with each digit that fits and recurses; on a dead 58 note end it empties the cell again (backtracking); the recursion is at most 81 deep 59 fn solve c ulong to bool 60 p = c 61 while lt p 81 bound 81 62 if eq get grid p 0 63 break 64 end 65 p = add p 1 66 end 67 if eq p 81 68 ret true 69 end 70 for x ulong 9 71 d = add x 1 72 if ok p d 73 put grid p d 74 if solve add p 1 75 ret true 76 end 77 end 78 end 79 put grid p 0 80 ret false 81 end 82 fn show to void 83 for r ulong 9 84 if and gt r 0 eq rem r 3 0 85 emit tbar 86 end 87 for k ulong 9 88 if and gt k 0 eq rem k 3 0 89 emit tsep 90 end 91 emitu get grid add mul r 9 k 92 if lt k 8 93 emit sp 94 end 95 end 96 emit nl 97 end 98 end 99 fn main to int 100 for i ulong 81 101 put grid i sub ulong get puzzle i 48 102 end 103 show 104 if not solve 0 105 exit 1 106 end 107 emit tsolved 108 show 109 ret 0 110 end