-
Notifications
You must be signed in to change notification settings - Fork 23
/
testSudoku.py
159 lines (136 loc) · 4.55 KB
/
testSudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python3 -u
from guy import Guy
import random
##################################################### my simplest sudoku resolver ;-)
free = lambda n: set("123456789.") ^ set(n)
carre = lambda g,x,y: g[y*9+x:y*9+x+3] + g[y*9+x+9:y*9+x+12] + g[y*9+x+18:y*9+x+21]
inter = lambda g,x,y: free(g[x::9]) & free(g[y*9:y*9+9]) & free(carre(g,(x//3)*3,(y//3)*3))
tri = lambda k: len(k[0])
def resolv(x):
idxs = sorted([(inter(x,i%9,i//9),i) for i,c in enumerate(x) if c=='.'],key=tri)
if not idxs: return x
for c in idxs[0][0]:
ng=resolv(x[:idxs[0][1]] + c + x[idxs[0][1]+1:])
if ng: return ng
#####################################################
class Sudoku(Guy):
"""
<style>
body {margin:0px;text-align:center;background:buttonface}
div#grid {
margin:8px;
border:2px solid black;
display:inline-block;
}
body.bad input {
color:red;
}
body.bad button#r {
pointer-events:none;
color:#FFF;
}
div#grid > input:read-only {
color:#AAA;
}
div#grid > input {
text-align: center;
font-size: 30px;
width:40px;
height:40px;
display:block;
border: 1px solid #ccc;
float: left;
}
div#grid > input:nth-child(9n+4), div#grid > input:nth-child(9n+7) {
border-left:2px solid black;
}
div#grid > input:nth-child(19),div#grid > input:nth-child(20),div#grid > input:nth-child(21),div#grid > input:nth-child(22),div#grid > input:nth-child(23),div#grid > input:nth-child(24),div#grid > input:nth-child(25),div#grid > input:nth-child(26),div#grid > input:nth-child(27),div#grid > input:nth-child(46),div#grid > input:nth-child(47),div#grid > input:nth-child(48),div#grid > input:nth-child(49),div#grid > input:nth-child(50),div#grid > input:nth-child(51),div#grid > input:nth-child(52),div#grid > input:nth-child(53),div#grid > input:nth-child(54) {
border-bottom:2px solid black;
}
div#grid > input:nth-child(9n+1) {
clear:both;
}
</style>
<div id="grid"></div>
<br/>
<button onclick="doClear()">Clear</button>
<button onclick="doRandom()">Random</button>
<button id="r" onclick="doResolv()">Resolv</button>
<script>
function setGrid(g) {
document.body.className="";
let d = document.querySelector("#grid")
d.innerHTML=""
for(var i=1;i<=9*9;i++) {
let c=g[i-1];
let h=document.createElement("input")
h.id=`c${i}` ;
if(c==".") {
h.onclick=function() {this.select()}
h.onchange=function() {doValid()}
}
else {
h.value=c
h.readOnly= true
}
d.appendChild( h )
}
undo = null;
}
function getGrid() {
var g="";
for(var i=1;i<=9*9;i++) {
let c=document.querySelector(`#c${i}`).value.trim()
g+=(c && "123456789".indexOf(c)>=0?c[0]:".");
}
return g
}
function doClear() {
setGrid(".................................................................................")
}
async function doValid() {
let err=await self.checkValid( getGrid() )
document.body.className=err?"bad":"";
}
async function doRandom() {
setGrid( await self.random() )
}
var undo=null;
async function doResolv() {
if(undo==null) {
let current = getGrid()
let r=await self.resolv( current )
if(r) {
setGrid( r )
undo = current;
}
}
else
setGrid(undo);
}
</script>
"""
size=(420,450)
async def init(self):
await self.js.setGrid( self.random() )
def resolv(self,g):
gr=resolv(g)
print("RESOLV: %s" % g)
print("----->: %s" % gr)
return gr
def random(self):
ll=80*["."] + [ str(random.randint(1,9)) ]
random.shuffle(ll)
ll=list(resolv("".join(ll)))
for i in range(100):
ll[ random.randint(0,80) ]="."
return "".join(ll)
def checkValid(self,g):
check9=lambda g: all([g.count(c)==1 for c in g.replace(".","")])
for i in range(0,9):
if not check9( g[i::9] ): return "Vertical trouble column %s"%i
if not check9( g[i*9:i*9+9] ): return "Horiz trouble row %s"%i
if not check9( carre(g,(i*3)%9,(i//3)*3) ): return "Trouble in %s square"%i
if __name__=="__main__":
app=Sudoku()
app.run()