sudoku solver
This is a python script to solve sudoku puzzles - in particular a 16x16 board but with some small modifications it should be able to solve 9x9 boards too. It walks through the board evaluating each cell against six tests - first it removes any proven negatives from the cells list of possible values in it's row, column and square neighbours - then it checks against the same neighbours to see if the cell has any unique possibilities amongst them.
The code's a bit messier than I would like and I suspect could be optimised some what but as it completes on my desktop in 0.38 seconds I don't really thinks its worth the effort. It take 21 iterations of the board, running a total of 9247 individual tests.
### GC2RBNM
import time
#The board is defined as an two dimensional array of ints with -1 representing a blank cell.
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
game = {
'board' : [
[ 7, -1, 14, 10, -1, -1, -1, 12, -1, 13, -1, -1, -1, 8, 6, 1 ], # F
[ 6, -1, -1, -1, -1, -1, -1, -1, 16, 4, -1, 8, -1, 7, 3, 12 ], # E
[ 16, -1, -1, 8, -1, -1, 6, -1, 3, -1, -1, 7, -1, 4, -1, 14 ], # D
[ -1, 4, -1, 9, 8, -1, -1, -1, -1, 6, -1, -1, -1, 5, -1, -1 ], # C
[ -1, -1, -1, -1, -1, -1, -1, 6, -1, -1, 8, 16, -1, -1, 13, -1 ], # B
[ 9, -1, 12, 4, 2, 11, -1, 16, 7, 5, 6, -1, 8, -1, -1, -1 ], # A
[ -1, -1, 3, 6, -1, -1, 13, 8, -1, -1, -1, 12, -1, 11, -1, -1 ], # 9
[ -1, -1, 8, 11, -1, -1, -1, 7, 1, -1, -1, 3, 6, 12, -1, 2 ], # 8
[ 15, -1, 11, -1, 16, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, 8 ], # 7
[ 1, -1, 6, 7, -1, 5, 2, -1, -1, 8, -1, -1, 3, 9, -1, 4 ], # 6
[ 14, -1, -1, -1, 15, 8, 12, 11, -1, 10, -1, -1, 7, 6, -1, -1 ], # 5
[ 8, -1, -1, 16, -1, -1, 3, 9, -1, -1, -1, 6, 5, -1, 14, -1 ], # 4
[ -1, -1, -1, 5, 6, -1, 1, 14, 15, -1, -1, 11, 4, -1, 8, 3 ], # 3
[ -1, -1, -1, -1, 13, -1, 8, -1, 6, -1, -1, 1, -1, 2, 7, -1 ], # 2
[ -1, 6, 15, -1, -1, 2, -1, -1, 8, 3, -1, 9, 1, -1, -1, 10 ], # 1
[ -1, 8, -1, 2, -1, -1, -1, 3, -1, 16, 14, 10, -1, -1, 9, 6 ] # 0
],
'possible' : [[]], # list of possibilities - one for each cell
'changed' : [[]] # changed flag - one for each cell
}
#init the arrays
for x in range(16): # add a row
game['possible'].append([])
game['changed'].append([])
for y in range(16):
game['possible'][x].append(range(1,16+1))
game['changed'][x].append(False)
#print the board - uses ansi colours to show updated
def printgame():
CSI="\x1B["
for x in range(16):
for y in range(16):
cell = game['board'][x][y]
if cell == -1:
cell = ''
cell = str(cell)
if game['changed'][x][y] == True:
#color it
cell = '\033[93m' + cell.rjust(4) + '\033[0m'
game['changed'][x][y] = False #clear it
else:
cell = cell.rjust(4)
print cell,
print
#walk the column removing possibilities that have been proven
def checkcellx(x, y):
for i in range(0,16):
if game['board'][x][i] != -1:
if game['board'][x][i] in game['possible'][x][y]:
game['possible'][x][y].remove(game['board'][x][i])
#walk the row removing possibilities that have been proven
def checkcelly(x, y):
for i in range(0,16):
if game['board'][i][y] != -1:
if game['board'][i][y] in game['possible'][x][y]:
game['possible'][x][y].remove(game['board'][i][y])
#walk the square removing possibilities that have been proven
def checksquare(x, y):
minx = x-(x-(4*(x/4)))
maxx = minx+4
miny = y-(y-(4*(y/4)))
maxy = miny+4
for xi in range(minx, maxx):
for yi in range(miny, maxy):
#print xi, yi
if game['board'][xi][yi] != -1:
if game['board'][xi][yi] in game['possible'][x][y]:
game['possible'][x][y].remove(game['board'][xi][yi])
#walk the col checking if we have any unique possibities
def checkcellx2(x, y):
c = { }
for i in range(0,16):
for possible in game['possible'][x][i]:
for poss in game['possible'][x][y]:
if poss == possible:
c[str(possible)] = c.get(str(possible), 0) + 1
if c[str(possible)] > 1:
break
for a in c:
if c[a] == 1:
game['possible'][x][y] = [int(a)]
#walk the row checking if we have any unique possibities
def checkcelly2(x, y):
c = { }
for i in range(0,16):
for possible in game['possible'][i][y]:
for poss in game['possible'][x][y]:
if poss == possible:
c[str(possible)] = c.get(str(possible), 0) + 1
if c[str(possible)] > 1: #optimisation - break out early if we're not the only one
break
for a in c:
if c[a] == 1: #if we are the only one then update the game board
game['possible'][x][y] = [int(a)]
#walk the square checking if we have any unique possibities
def checksquare2(x, y):
c = { }
minx = x-(x-(4*(x/4)))
maxx = minx+4
miny = y-(y-(4*(y/4)))
maxy = miny+4
for xi in range(minx, maxx):
for yi in range(miny, maxy):
for possible in game['possible'][xi][yi]:
for poss in game['possible'][x][y]:
if poss == possible:
c[str(possible)] = c.get(str(possible), 0) + 1
if c[str(possible)] > 1:
break
for a in c:
if c[a] == 1:
game['possible'][x][y] = [int(a)]
printgame()
print
print
start = time.time()
testsrun = 0
runnum = 0
update = True
while update:
#print runnum
update = False
for x in range(16):
for y in range(16):
if game['board'][x][y] == -1:
#print "x:",x," y:",y,
#possible optimisation - dont run more tests if already proven in previous test
#(probably not worth it as it only saves ~350 tests and duration is approx the same)
if len(game['possible'][x][y]) != 1:
checkcellx(x,y)
testsrun = testsrun + 1
if len(game['possible'][x][y]) != 1:
checkcelly(x,y)
testsrun = testsrun + 1
if len(game['possible'][x][y]) != 1:
checksquare(x,y)
testsrun = testsrun + 1
if len(game['possible'][x][y]) != 1:
checkcellx2(x,y)
testsrun = testsrun + 1
if len(game['possible'][x][y]) != 1:
checkcelly2(x,y)
testsrun = testsrun + 1
if len(game['possible'][x][y]) != 1:
checksquare2(x,y)
testsrun = testsrun + 1
if len(game['possible'][x][y]) == 1: #only one possibility left so its now proven
#print "x:",x," y:",y, "update:", game['possible'][x][y]
game['board'][x][y] = game['possible'][x][y][0]
update = True
game['changed'][x][y] = True
else:
game['possible'][x][y] = [game['board'][x][y]]
runnum = runnum + 1
elapsed = (time.time() - start)
print
printgame()
print "runs#".ljust(10),runnum
print "tests#".ljust(10),testsrun
print "elapsed".ljust(10),elapsed