-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_solver.py
158 lines (127 loc) · 3.8 KB
/
sudoku_solver.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
import pandas as pd
class SudokuData:
def __init__(self, file):
self.data = pd.read_csv(file)
def _get_index(self, df):
res = set()
for index, row in df.iterrows():
res.add( row['index'] )
return res
def getRow(self, num):
row = self.data[ self.data['index'] == num ]['row'].values[0]
tmp = self.data[ self.data['row'] == row ]
return self._get_index(tmp)
def getCol(self, num):
col = self.data[ self.data['index'] == num ]['col'].values[0]
tmp = self.data[ self.data['col'] == col ]
return self._get_index(tmp)
def getSquare(self, num):
box = self.data[ self.data['index'] == num ]['box'].values[0]
tmp = self.data[ self.data['box'] == box ]
return self._get_index(tmp)
class Cell:
def __init__(self, number):
self.num = number
self.options = set()
if number == 0:
self.options = set(range(1, 10))
def remove(self, numbers):
self.options.difference_update(numbers)
def removeUnique(self, numbers):
unique = []
for option in self.options:
if option not in numbers:
unique.append(option)
if len(unique) == 1:
self.num = unique[0]
self.options = set()
def check(self):
if len(self.options) == 1:
self.num = self.options.pop()
def __str__(self) -> str:
if self.num == 0:
return '_'
else:
return str(self.num)
class Sudoku:
def __init__(self, matrix):
self.matrix = []
self.data = SudokuData('data/sudoku_data.csv')
if len(matrix) != 81:
return
for m in matrix:
self.matrix.append( Cell(m) )
def _getNumbers(self, index):
row = set()
for i in index:
if self.matrix[i].num != 0:
row.add( self.matrix[i].num )
return row
def getSquare(self, num):
index = self.data.getSquare(num)
return self._getNumbers(index)
def getRow(self, num):
index = self.data.getRow(num)
return self._getNumbers(index)
def getCol(self, num):
index = self.data.getCol(num)
return self._getNumbers(index)
def count(self):
qty = 0
for m in self.matrix:
if m.num != 0:
qty += 1
return qty
def check(self):
for i in range(0, len(self.matrix)):
if self.matrix[i].num != 0:
continue
rms = self.getSquare(i).union(self.getRow(i)).union(self.getCol(i))
self.matrix[i].remove( rms )
self.matrix[i].check()
return len(self.matrix) - self.count()
def uniqueOption(self):
for i in range(0, len(self.matrix)):
if self.matrix[i].num != 0:
continue
indexs = self.data.getSquare(i)
option = set()
for index in indexs:
if self.matrix[index].num != 0 or index == i:
continue
option = option.union( self.matrix[index].options )
self.matrix[i].removeUnique( option )
if self.matrix[i].num != 0:
continue
indexs = self.data.getRow(i)
option = set()
for index in indexs:
if self.matrix[index].num != 0 or index == i:
continue
option = option.union( self.matrix[index].options )
self.matrix[i].removeUnique( option )
if self.matrix[i].num != 0:
continue
indexs = self.data.getCol(i)
option = set()
for index in indexs:
if self.matrix[index].num != 0 or index == i:
continue
option = option.union( self.matrix[index].options )
self.matrix[i].removeUnique( option )
return len(self.matrix) - self.count()
def __str__(self) -> str:
qty = 0
res = ''
for m in self.matrix:
res += str(m) + ' '
qty += 1
if qty%3 == 0:
res += ' '
if qty%9 == 0:
res = res[:-3]
res += "\n"
if qty%27 == 0:
res += "\n"
res = res[:-2]
return res