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/drafts/rosettacode/sudoku.wiki
# 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 / drafts / rosettacode / sudoku.wiki`
139 lines ยท 2613 bytes Raw Blame History
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 (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text puzzle "`850002400720000009004000000000107002305000900040000000000080070017000000000036040`"
10 text tbar "------+-------+------\n"
11 text tsep "| "
12 text tsolved "solved:\n"
13 global grid ulong 81
14 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15 fn emit a view uchar * to void
16 o = ulong 0
17 while lt o len a bound 4096
18 k = write 1 a o sub len a o
19 if le k 0
20 exit 1
21 end
22 o = add o ulong k
23 end
24 end
25 note emitu writes n in decimal; 20 digits hold every ulong
26 fn emitu n ulong to void
27 d = new uchar 20 0
28 i = ulong 20
29 v = n
30 while true bound 20
31 i = sub i 1
32 put d i uchar add rem v 10 48
33 v = div v 10
34 if eq v 0
35 break
36 end
37 end
38 emit part d i sub 20 i
39 end
40 note ok says whether digit d may go in cell c: not already in its row, column or 3x3 box
41 fn ok c ulong d ulong to bool
42 r = div c 9
43 k = rem c 9
44 b = add mul mul div r 3 3 9 mul div k 3 3
45 for i ulong 9
46 if eq get grid add mul r 9 i d
47 ret false
48 end
49 if eq get grid add mul i 9 k d
50 ret false
51 end
52 q = add b add mul div i 3 9 rem i 3
53 if eq get grid q d
54 ret false
55 end
56 end
57 ret true
58 end
59 note solve fills the first empty cell at or after c with each digit that fits and recurses; on a dead
60 note end it empties the cell again (backtracking); the recursion is at most 81 deep
61 fn solve c ulong to bool
62 p = c
63 while lt p 81 bound 81
64 if eq get grid p 0
65 break
66 end
67 p = add p 1
68 end
69 if eq p 81
70 ret true
71 end
72 for x ulong 9
73 d = add x 1
74 if ok p d
75 put grid p d
76 if solve add p 1
77 ret true
78 end
79 end
80 end
81 put grid p 0
82 ret false
83 end
84 fn show to void
85 for r ulong 9
86 if and gt r 0 eq rem r 3 0
87 emit tbar
88 end
89 for k ulong 9
90 if and gt k 0 eq rem k 3 0
91 emit tsep
92 end
93 emitu get grid add mul r 9 k
94 if lt k 8
95 emit sp
96 end
97 end
98 emit nl
99 end
100 end
101 fn main to int
102 for i ulong 81
103 put grid i sub ulong get puzzle i 48
104 end
105 show
106 if not solve 0
107 exit 1
108 end
109 emit tsolved
110 show
111 ret 0
112 end
113
114 {{out}}
115
116 8 5 0 | 0 0 2 | 4 0 0
117 7 2 0 | 0 0 0 | 0 0 9
118 0 0 4 | 0 0 0 | 0 0 0
119 ------+-------+------
120 0 0 0 | 1 0 7 | 0 0 2
121 3 0 5 | 0 0 0 | 9 0 0
122 0 4 0 | 0 0 0 | 0 0 0
123 ------+-------+------
124 0 0 0 | 0 8 0 | 0 7 0
125 0 1 7 | 0 0 0 | 0 0 0
126 0 0 0 | 0 3 6 | 0 4 0
127 solved:
128 8 5 9 | 6 1 2 | 4 3 7
129 7 2 3 | 8 5 4 | 1 6 9
130 1 6 4 | 3 7 9 | 5 2 8
131 ------+-------+------
132 9 8 6 | 1 4 7 | 3 5 2
133 3 7 5 | 2 6 8 | 9 1 4
134 2 4 1 | 5 9 3 | 7 8 6
135 ------+-------+------
136 4 3 2 | 9 8 1 | 6 7 5
137 6 1 7 | 4 2 5 | 8 9 3
138 5 9 8 | 7 3 6 | 2 4 1
139