interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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