Sei sulla pagina 1di 7

#data = "123456789....5...91...5...91...5...91...5...91...5...91...5...91...5...

91...5...9"
data = "1.6...7...4.3...6.....4.1.2.1...9.2...5.6.3...3.5...9.4.8.7.....6...8.1.
..1...4.6" #could not solve
#data = "123......456................8..........................................
.8........"
#data = "00000001040000000002000000000005040700800030000109000030040020005010000
0000806000" #solved
#data = "00000001040000000002000000000005060400800030000109000030040020005010000
0000807000" #solved
#data = "00000001200003500000060007070000030000040080010000000000012000008000004
0050000600" #solved
#data = "00000001200360000000000700041002000000050030070000060028000004000030050
0000000000" #solved
#data = "00000001200803000000000004012050000000000470006000000050700030000062000
0000100000" #solved
#data = "00000001204005000000000900007060040000010000000000005000008750060100030
0200000000" #solved
#data = "00000001205040000000000003070060040000100000000008000092000080000051070
0000003000" #solved
#data = "00000001230000006000004000090000050000000107002000000000035040000140080
0060000000" #solved
#data = "00000001240009000000000005007020000060000040000010800001800000000003070
0502000000" #could not solve
#data = "00000001250000800000070000060012000070000045000003000003000080000050070
0020000000" #solved
#data = "00000001270006000000000005008020000060000040000010900001900000000003080
0502000000" #could not solve
#data = "00000001280004000000000006009020000070000040000050100001500000000003090
0602000000" #could not solve
#data = "00000001298000000000060000010070008040200000000030060007000030005004000
0000010000" #solved
#data = "00000001300003008007000000000020600003000090000001000060050020400040070
0100000000" #solved
#data = "00000001300020000000000008000076020000800040001000000020000075060034000
0000008000" #solved
#data = "00000001300050007000080200000040090010700000000000020089000005004000060
0000010000" #solved
#data = "00000001300070006000050800000040080010600000000000020074000005002000040
0000010000" #solved
#data = "00000001300070006000050900000040090010600000000000020074000005008000040
0000010000" #solved
#data = "00000001300080007000050200000040090010700000000000020089000005004000060
0000010000" #solved
#data = "00000001302050000000000000010300007000080200000400000000034050067000020
0000010000" #solved
def displayGrid(style = 1):
for row in range(9):
line = " "
for col in range(9):
if style == 1:
if len(matrix[row][col]) == 1:
line = line + (matrix[row][col])
else:
line = line + (".")
line += " "
elif style == 2:
print str(matrix[row][col]).center(17),
print line
print
def initMatrix(data):
global matrix
if len(data) != 81:
print "Error - data needs to be a single string of 81 character with per
iods for unknowns"
for row in range(9):
for col in range(9):
num = data[:1]
data = data[1:]
#print num
if num != "." and num != "0":
matrix[row][col] = num
updateNeighbors(row, col)
def updateNeighbors(row, col):
global changed
changed = True
#print "trying", matrix[row][col]
num = int(matrix[row][col])
#print num, "updating neighbors of %d-%d" % (row, col)
#update row
for i in range(9):
if len(matrix[i][col]) > 1:
rem(num, i, col, matrix)
#update column
for j in range(9):
if len(matrix[row][j]) > 1:
rem(num, row, j, matrix)
#update box
#something with ceiling of row/3?
boxrow = row/3
boxcol = col/3
for i in range(boxrow*3, boxrow*3+3):
#print "row:", i,
for j in range(boxcol*3, boxcol*3+3):
#print "col:", j,
if len(matrix[i][j]) > 1:
rem(num, i, j, matrix)

#print "end update"


#displayGrid()
#test = raw_input("")
def rem(num, row, col, matrix):
global changed
if len(matrix[row][col]) > 1 and matrix[row][col].count(num) == 1:
changed = True
matrix[row][col].remove(num)
#print "%d removed from %d-%d" %(num,row,col)
if len(matrix[row][col]) == 1:
print "THIS ONE IS NOW A SINGLETON!", matrix[row][col]
matrix[row][col] = "%d" %matrix[row][col][0]
#print matrix[row][col]
updateNeighbors(row,col)
def solveSingletons():
for row in range(9):
for col in range(9):
if len(matrix[row][col]) == 1:
pass
#print row, col, "SINGLETON FOUND"
#this finds all solved cells as well though?
def checkRows():
for row in range(9):
for num in range(1,10):
# numExists = False
# numOnList = False
numIndex = -1
for col in range(9):
#print matrix[row][col], num, "test"
# if matrix[row][col] == num: #does this need become an int to c
ompare properly?
# numExists = True
# print "numExists"
# break
if len(matrix[row][col]) > 1 and matrix[row][col].count(num) ==
1:
if numIndex == -1:
numIndex = col
#print "numIndex:", numIndex
elif numIndex > -1:
#print "multiple %d" %num
numIndex = -1
break
#print "numOnList"
#print "end column check for num:%d, row:%d" %(num,row)
if numIndex > -1:
#print "can put %d at %d-%d" %(num, row, numIndex)
matrix[row][numIndex] = "%d" %num
updateNeighbors(row,numIndex)
#bill = raw_input("")
#print "end num check %d, %d-%d" %(num,row,col)
#print "end row check %d, %d-%d" %(num,row,col)
def checkColumns():
for col in range(9):
for num in range(1,10):
# numExists = False
# numOnList = False
numIndex = -1
for row in range(9):
#print matrix[row][col], num, "test"
# if matrix[row][col] == num: #does this need become an int to c
ompare properly?
# numExists = True
# print "numExists"
# break
if len(matrix[row][col]) > 1 and matrix[row][col].count(num) ==
1:
if numIndex == -1:
numIndex = row
#print "numIndex:", numIndex
elif numIndex > -1:
#print "multiple %d" %num
numIndex = -1
break
#print "numOnList"
#print "end row check for num:%d, col:%d" %(num,col)
if numIndex > -1:
#print "can put %d at %d-%d" %(num, numIndex, col)
matrix[numIndex][col] = "%d" %num
updateNeighbors(numIndex, col)
#print "end num check %d, %d-%d" %(num,row,col)
#print
#print "end col check %d, %d-%d" %(num,row,col)

def checkBoxes():
for boxrow in range(3):
for boxcol in range(3):
#this gets each box tested - now within each box...
for num in range(1,10):
numIndex = -1
for row in range(boxrow*3, boxrow*3+3):
for col in range(boxcol*3, boxcol*3+3):
#this tests each cell within a box
#print "testing box", row, col
if len(matrix[row][col]) > 1 and matrix[row][col].count(
num) == 1:
if numIndex == -1:
numIndex = (row, col)
#print "numIndex:", numIndex
elif numIndex != -1:
#print "multiple %d" %num
numIndex = -2
break
#print "numOnList"
#print "numIndex1:", numIndex
#print "numIndex2:", numIndex
#print "numIndex3:", numIndex
if numIndex == -2:
break
#print "end check box for num %d" %num
if numIndex != -1 and numIndex != -2:
#print "can put %d at %d-%d" %(num, numIndex[0], numIndex[1]
)
matrix[numIndex[0]][numIndex[1]] = "%d" %num
updateNeighbors(numIndex[0],numIndex[1])
def addPos(posList, line):
#add line to list, but not if already there
if posList.count(line) == 0:
posList.append(line)
return posList
def sliceAndDice():
#in each 3x3 block, check if a candidate occurs only in one column or row
#if so we can remove that number from the rest of the row or column outside
# the block
for boxrow in range(3):
for boxcol in range(3):
for num in range(1,10):
canRow = []
canCol = []
for row in range(boxrow*3,boxrow*3+3):
for col in range(boxcol*3,boxcol*3+3):
if len(matrix[row][col]) > 1 and matrix[row][col].count(
num) == 1:
canRow = addPos(canRow, row)
canCol = addPos(canCol, col)
if len(canRow) == 1:
#print "this row is a possibility %s for num %d" %(canRow, n
um)
changed = True
removeOtherSix("r", num, canRow, boxcol)
if len(canCol) == 1:
#print "this column is a possibility %s for num %d" %(canCol
, num)
changed = True
removeOtherSix("c", num, canCol, boxrow)
def removeOtherSix(rowOrCol, num, pos, box):
pos = pos[0]
if rowOrCol == "r":
print "Removing %d from remaining columns in row %d not in box %d" %(num
, pos, box)
if box == 0:
lines = [3,4,5,6,7,8]
elif box == 1:
lines = [0,1,2,6,7,8]
elif box == 2:
lines = [0,1,2,3,4,5]
for col in lines:
rem(num, pos, col, matrix)
elif rowOrCol == "c":
print "Removing %d from remaining rows in column %d not in box %d" %(num
, pos, box)
if box == 0:
lines = [3,4,5,6,7,8]
elif box == 1:
lines = [0,1,2,6,7,8]
elif box == 2:
lines = [0,1,2,3,4,5]
for row in lines:
rem(num, row, pos, matrix)
else:
print "ERROR - invalid argument"
def findDoubles():
#search rows and columns for cells with the same two possibilities -
#this means these two cells must contain these two value and any other cells
in the row can be simplified
for row in range(9):
pairs, loc, num1, num2, rows = [], [], [], [], []
for col in range(9):
if len(matrix[row][col]) == 2:
pairs.append(matrix[row][col])
loc.append(col)
print "pairs in row %d:" %row, pairs
#need to check pairs - if two are equal can simplify possibilities
for i, pair in enumerate(pairs):
if pairs.count(pair) == 2:
print "there are two of these pairs", pair
num1 = pair[0]
num2 = pair[1]
rows.append(loc[i])
#remove the other numbers - make sure not to remove from the pai
rs
if len(rows) > 0:
print num1, num2, rows
for col in range(9):
if rows.count(col) == 0:
rem(num1, row, col, matrix)
rem(num2, row, col, matrix)
bill = raw_input("")
for col in range(9):
pairs, loc, num1, num2, rows = [], [], [], [], []
for row in range(9):
if len(matrix[row][col]) == 2:
pairs.append(matrix[row][col])
loc.append(row)
print "pairs in col %d:" %col, pairs
#need to check pairs - if two are equal can simplify possibilities
for i, pair in enumerate(pairs):
if pairs.count(pair) == 2:
print "there are two of these pairs", pair
num1 = pair[0]
num2 = pair[1]
rows.append(loc[i])
#remove the other number - make sure not to remove from the pair
s
if len(rows) > 0:
print num1, num2, rows
for row in range(9):
if rows.count(row) == 0:
rem(num1, row, col, matrix)
rem(num2, row, col, matrix)
bill = raw_input("")
def checkDone():
for row in range(9):
for col in range(9):
if len(matrix[row][col]) != 1:
return False
return True
#main
matrix = [[range(1,10) for i in range(9)] for i in range(9)]
initMatrix(data)
displayGrid()
print
choice = "y"
changed = True
while choice == "y" and changed == True:
changed = False
solveSingletons()
checkBoxes()
checkRows()
checkColumns()
displayGrid()
if checkDone():
print "YAY - Solved!"
break
if changed == False:
sliceAndDice()
if changed == False:
findDoubles()
print changed
choice = "y"
#choice = raw_input("try again?")
if not checkDone():
print "Grid could not be solved - here are leftover possibilities:"
print
displayGrid(2)

Potrebbero piacerti anche