-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsudoku.py
executable file
·414 lines (320 loc) · 11.4 KB
/
sudoku.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Created by Izidor "iyonius" Matušov <izidor.matusov@gmail.com>
# on 29.12.2010
import sys
import copy
import logging
logger = logging.getLogger()
class SudokuFileException(Exception): pass
def readSudoku(fileName):
""" Parse file with sudoku """
sudoku = []
f = open(fileName, "r")
for line in f.readlines():
line = line.strip()
if len(line) != 9:
raise SudokuFileException("Each line must have 9 items")
lineItems = []
for item in line:
if ord('1') <= ord(item) <= ord('9'):
lineItems.append( (ord(item) - ord('0'), None) )
else:
lineItems.append( (None, [1,2,3,4,5,6,7,8,9]) )
sudoku.append(lineItems)
if len(sudoku) != 9:
raise SudokuFileException("Sudoku should have 9 lines")
return sudoku
def printBoard(board):
""" Print current state of board """
hSpace = 3
for line in board:
output = ""
vSpace = 3
for number, possible in line:
if vSpace <= 0:
output += " "
vSpace = 3
vSpace -= 1
if number is None:
output += "."
else:
output += str(number)
if hSpace <= 0:
print("")
hSpace = 3
hSpace -= 1
print(output)
def printPossible(board):
for y, line in enumerate(board):
for x, (number, possible) in enumerate(line):
if number is None:
print("[%d][%d] ?: %s" % (y, x,possible))
print()
def isUniqueList(l):
""" Each item should be in the list just once """
for item in l:
if l.count(item) != 1:
return False
return True
def checkInRows(board):
""" There should not be two same numbers in one row """
for y, line in enumerate(board):
numbers = [num for num, pn in line if num is not None]
if not isUniqueList(numbers):
return False
return True
def checkInCols(board):
""" There should not be two same numbers in one column """
for x in range(len(board[0])):
numbers = []
for line in board:
num, pn = line[x]
if num is not None:
numbers.append(num)
if not isUniqueList(numbers):
return False
return True
def checkInOneSqr(board, dy, dx):
""" Each number should be unique in a square """
numbers = []
for y in range(3):
for x in range(3):
num, pn = board[dy+y][dx+x]
if num is not None:
numbers.append(num)
return isUniqueList(numbers)
def checkInSqrs(board):
""" Check each square """
for dy in range(0, 9, 3):
for dx in range(0, 9, 3):
if not checkInOneSqr(board, dy, dx):
return False
return True
def checkBoard(board):
""" Is board in correct state? Is no rule broken? """
return checkInRows(board) and checkInCols(board) and checkInSqrs(board)
def isSolved(board):
""" Check if everything has been solved """
for line in board:
for number, possible in line:
if number is None:
return False
return True
def listDifference(a, b):
""" Make a-b on lists """
return list(set(a).difference(set(b)))
def repairPossibleNumbersInRows(board):
""" Remove possible values by checking in a row """
for y, line in enumerate(board):
numbers = [num for num, pn in line if num is not None]
for x, (num, possibleNumbers) in enumerate(line):
if num is None:
board[y][x] = (num, listDifference(possibleNumbers, numbers))
return board
def repairPossibleNumbersInCols(board):
""" Remove possible values by checking in a column """
for x in range(len(board[0])):
numbers = []
for line in board:
num, pn = line[x]
if num is not None:
numbers.append(num)
for line in board:
num, pn = line[x]
if num is None:
line[x] = (num, listDifference(pn, numbers))
return board
def repairPossibleNumbersInOneSqr(board, dy, dx):
""" Remove possible values by checking in a square """
numbers = []
for y in range(3):
for x in range(3):
num, pn = board[dy+y][dx+x]
if num is not None:
numbers.append(num)
for y in range(3):
for x in range(3):
num, pn = board[dy+y][dx+x]
if num is None:
board[dy+y][dx+x] = (num, listDifference(pn, numbers))
return board
def repairPossibleNumbersInSqrs(board):
""" Check each square """
for dy in range(0, 9, 3):
for dx in range(0, 9, 3):
board = repairPossibleNumbersInOneSqr(board, dy, dx)
return board
def repairPossibleNumbers(board):
""" Update possible values """
board = repairPossibleNumbersInRows(board)
board = repairPossibleNumbersInCols(board)
board = repairPossibleNumbersInSqrs(board)
return board
def listCommonItem(a, b):
""" Check if two list has just one common item """
result = list(set(a).intersection(set(b)))
if len(result) != 1:
return None
else:
return result[0]
def fillNumbersInRows(board):
""" Insert missing numbers into a row """
changed = False
for y, line in enumerate(board):
numbers = [num for num, pn in line if line.count(num) == 1]
for x, (num, pn) in enumerate(line):
if num is None and listCommonItem(pn, numbers) is not None:
num = listCommonItem(pn, numbers)
logger.debug("FillRow change at [%d][%d] to %d" % (y, x, num))
line[x] = (num, None)
changed = True
return changed, board
def fillNumbersInCols(board):
""" Insert missing numbers into a column """
changed = False
for x in range(len(board[0])):
counts = [0] * 10
for line in board:
num, pn = line[x]
if num is None:
for possible in pn:
counts[possible] += 1
numbers = [num for num, count in enumerate(counts) if count == 1]
for y, line in enumerate(board):
num, pn = line[x]
if num is None and listCommonItem(pn, numbers) is not None:
num = listCommonItem(pn, numbers)
logger.debug("FillCol change at [%d][%d] to %d" % (y, x, num))
line[x] = (num, None)
changed = True
return changed, board
def fillInOneSqr(board, dy, dx):
""" Insert missing numbers into a square """
occurence = []
changed = False
for y in range(3):
for x in range(3):
num, pn = board[dy+y][dx+x]
if num is None:
occurence += pn
numbers = [num for num in occurence if occurence.count(num) == 1]
for y in range(3):
for x in range(3):
num, pn = board[dy+y][dx+x]
if num is None and listCommonItem(pn, numbers) is not None:
num = listCommonItem(pn, numbers)
logger.debug("FillSqr change at [%d][%d] to %d" % (y+dy, x+dx, num))
board[y+dy][x+dx] = (num, None)
changed = True
return changed, board
def fillNumbersInSqrs(board):
""" Check each square for filling """
change = False
for dy in range(0, 9, 3):
for dx in range(0, 9, 3):
changeInSqr, board = fillInOneSqr(board, dy, dx)
change = change or changeInSqr
return change, board
def fillSureNumbers(board):
""" Fill items with just one possible number """
changed = False
for y, line in enumerate(board):
for x, (number, possibleNumbers) in enumerate(line):
if number is None and len(possibleNumbers) == 1:
number = possibleNumbers[0]
board[y][x] = (number, None)
logger.debug("Change at [%d][%d] to %d" % (y,x,number))
changed = True
return changed, board
def solveSudoku(board):
""" Method for solving a sudoku puzzle
Try to solve the puzzle using heuristics.
If it fails, "guess" value by using
Depth-First Search algorithm.
"""
changed = True
while not isSolved(board) and changed:
if logger.isEnabledFor(logging.DEBUG):
print("-"*50)
printBoard(board)
print()
if not checkBoard(board):
return
changed = False
board = repairPossibleNumbers(board)
if logger.isEnabledFor(logging.DEBUG):
printPossible(board)
change, board = fillSureNumbers(board)
if change:
changed = True
board = repairPossibleNumbers(board)
if logger.isEnabledFor(logging.DEBUG):
printPossible(board)
change, board = fillNumbersInRows(board)
if change:
changed = True
board = repairPossibleNumbers(board)
if logger.isEnabledFor(logging.DEBUG):
printPossible(board)
change, board = fillNumbersInCols(board)
if change:
changed = True
board = repairPossibleNumbers(board)
if logger.isEnabledFor(logging.DEBUG):
printPossible(board)
change, board = fillNumbersInSqrs(board)
changed = changed or change
# End of main while
if isSolved(board):
yield board
return
# In other case, try to guess
# Firstly, find most problematic cell
# i.e. cell with the most possible values
chooseY, chooseX = 0, 0
chooseLength, options = None, None
for y, line in enumerate(board):
for x, (num, pn) in enumerate(line):
if num is None and (chooseLength is None or len(pn) > chooseLength):
chooseY, chooseX = y, x
options = pn
chooseLength = len(pn)
# Let's guess
for guess in options:
newBoard = copy.deepcopy(board)
assert newBoard[chooseY][chooseX][0] is None
newBoard[chooseY][chooseX] = (guess, None)
logger.debug("Guess [%d][%d] => %d" % (chooseY, chooseX, guess))
for solution in solveSudoku(newBoard):
yield solution
if __name__ == "__main__":
#logger.setLevel(logging.DEBUG)
if len(sys.argv) != 2:
print("Usage: %s <sudoku-file>" % sys.argv[0])
sys.exit(1)
board = readSudoku(sys.argv[1])
foundSolution = False
for solution in solveSudoku(board):
foundSolution = True
print("Solution")
print("========")
printBoard(solution)
print()
if not foundSolution:
print("Unable to find solution :-(")
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4