-
Notifications
You must be signed in to change notification settings - Fork 0
/
newSudoku.py
169 lines (150 loc) · 4.8 KB
/
newSudoku.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
import numpy as np
import time
def validNumber(number, rowNumber, columnNumber):
global sudoku
if number in sudoku[rowNumber]:
return 0
if number in sudoku[:,columnNumber]:
return 0
for row in sudoku[rowNumber//3*3:(rowNumber//3+1)*3,columnNumber//3*3:(columnNumber//3+1)*3]:
if number in row:
return 0
return 1
def updatePossibleNumbers():
for count in range(81):
i, j = count//9, count%9
if not sudokuState[i][j]:
possibleNumbers[count] = [num for num in possibleNumbers[count] if validNumber(num, i ,j)]
def insertNumber(count):
rowNumber, columnNumber = count//9, count%9
belongingBlocks = []
cellNumber = (rowNumber//3)*3 + columnNumber//3
firstNumber = 10+(cellNumber-1)*3 + (cellNumber//3)*18
# for k in range(rowNumber*9, (rowNumber+1)*9):
# belongingBlocks.append(k)
# for k in range(9+column, 73+column, 9):
# belongingBlocks.append(k)
# for k in firstNumber + np.array([0,1,9,10]):
# belongingBlocks.append(k)
for num in possibleNumbers[count]:
flag = 1
for k in range(0 + rowNumber*9, 9 + rowNumber*9):
# print (rowNumber)
# exit()
if k != count and num in possibleNumbers[k]:
flag = 0
break
if flag == 1:
return num
flag = 1
for k in range(columnNumber, 72 + columnNumber, 9):
if k != count and num in possibleNumbers[k]:
flag = 0
break
if flag == 1:
return num
return 0
def validNumber2(number, rowNumber, columnNumber):
global sudoku
# print (sudoku[rowNumber][columnNumber])
if sudoku[rowNumber][columnNumber] != 0:
if list(sudoku[rowNumber]).count(number) > 1:
print ("gone in row")
print (number, sudoku[rowNumber])
return 0
if list(sudoku[:,columnNumber]).count(number) > 1:
print ("gone in column")
return 0
for row in sudoku[rowNumber//3*3:(rowNumber//3+1)*3,columnNumber//3*3:(columnNumber//3+1)*3]:
if list(row).count(number) > 1:
print ("gone in block")
return 0
return 1
possibleNumbers = []
for count in range(81):
possibleNumbers.append([])
mat = input("enter numbers")
sudoku = []
for i in mat.split(" "):
row = []
# print (list(i))
for j in i:
row.append(int(j))
sudoku.append(row)
sudoku = np.array(sudoku)
print (sudoku)
count = 0
for row in sudoku:
count += np.count_nonzero(row)
print (count)
sudokuState = []
for row in sudoku:
state = []
for element in row:
if element == 0:
state.append(0)
else:
state.append(1)
sudokuState.append(state)
# print ('\n',np.array(sudokuState))
for count in range(81):
i, j = count//9, count%9
if not sudokuState[i][j]:
possibleNumbers[count] = [num for num in range(1,10) if validNumber(num, i ,j)]
if len(possibleNumbers[count]) == 1:
sudoku[i][j] = possibleNumbers[count][0]
sudokuState[i][j] = 1
del possibleNumbers[count][0]
# print ('\n',np.array(sudokuState))
print()
# for row in sudoku:
# print(row)
# # print()
# # for row in possibleNumbers:
# # print (row)
# for count in range(81):
# i, j = count//9, count%9
# if not validNumber2(sudoku[i][j], i, j):
# print("gone at",i,j)
# break
for iter in range(3):
for r in range (81):
print (possibleNumbers[r])
for count in range(81):
i, j = count//9, count%9
num = insertNumber(count)
if num != 0:
sudoku[i][j] = num
del possibleNumbers[count][:]
updatePossibleNumbers()
print (sudoku)
count = 0
for row in sudoku:
count += np.count_nonzero(row)
print (count)
state = 1
while state:
# updatePossibleNumbers()
state = 0
for count in range(81):
# print (count)
i, j = count//9, count%9
if not sudokuState[i][j]:
newValidNumbers = [num for num in possibleNumbers[count] if validNumber(num, i, j)]
possibleNumbers[count] = newValidNumbers
if len(possibleNumbers[count]) == 1:
# print ("state1")
sudoku[i][j] = possibleNumbers[count][0]
sudokuState[i][j] = 1
state = 1
# print ("here", count)
del possibleNumbers[count][0]
updatePossibleNumbers()
# print (possibleNumbers[count])
# break
print (sudoku)
count = 0
for row in sudoku:
count += np.count_nonzero(row)
print (count)
updatePossibleNumbers()