forked from KanishkM08/Sudo-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_validity_checker.py
146 lines (110 loc) · 3.9 KB
/
config_validity_checker.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
# __func() = Utility function
def __not_inrow(arr, row):
st = set()
for i in range(0, 9):
# If already encountered before
if arr[row][i] in st:
return False
# If not an empty cell, insert value at the current cell in set
if arr[row][i] not in [" ", 0]:
st.add(arr[row][i])
return True
# check duplicates in column
def __not_incol(arr, col):
st = set()
for i in range(0, 9):
# If already encountered before
if arr[i][col] in st:
return False
# If not an empty cell, insert value at the current cell in set
if arr[i][col] not in [" ", 0]:
st.add(arr[i][col])
return True
# check duplicates in 3x3 box
def __not_inbox(arr, startRow, startCol):
st = set()
for row in range(0, 3):
for col in range(0, 3):
curr = arr[row + startRow][col + startCol]
# If already encountered before,
# return false
if curr in st:
return False
# If it is not an empty cell,
# insert value at current cell in set
if curr not in [" ", 0]:
st.add(curr)
return True
# Get validity of board, and invalidity in ROW, COL, or BOX
def __is_valid(arr, row, col):
row_inv = not __not_inrow(arr, row)
col_inv = not __not_incol(arr, col)
box_inv = not __not_inbox(arr, row - row % 3, col - col % 3)
return (row_inv, col_inv, box_inv)
def currentconfig_isvalid(board, num, pos):
# Check row
for i in range(len(board[0])):
if board[pos[0]][i] == num and pos[1] != i:
return False
# Check column
for i in range(len(board)):
if board[i][pos[1]] == num and pos[0] != i:
return False
# Check box
box_x = pos[1] // 3
box_y = pos[0] // 3
for i in range(box_y * 3, box_y * 3 + 3):
for j in range(box_x * 3, box_x * 3 + 3):
if board[i][j] == num and (i, j) != pos:
return False
return True
def fullboard_isvalid(board):
for i in range(0, len(board)):
for j in range(0, len(board)):
row_inv, col_inv, box_inv = __is_valid(board, i, j)
if row_inv or col_inv or box_inv:
return (
False, # <is config valid?>
row_inv, # <invaldity in row?>
col_inv, # <invaldity in column?>
box_inv, # <invaldity in 3x3 box?>
)
return True, None, None, None
if __name__ == "__main__":
test_validboard = [
[0, 0, 0, 8, 0, 0, 4, 2, 0],
[5, 0, 0, 6, 7, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 9, 0, 0, 5],
[7, 4, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 9, 0, 3, 0, 7, 0, 0],
[0, 0, 0, 0, 0, 7, 0, 4, 8],
[8, 0, 0, 4, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 9, 8, 0, 0, 3],
[0, 9, 5, 0, 0, 3, 0, 0, 0],
]
test_invalidboard = [
[0, 0, 0, 8, 0, 0, 4, 2, 0],
[5, 0, 0, 6, 7, 0, 0, 0, 0],
[0, 5, 0, 0, 0, 9, 0, 0, 5],
[7, 4, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 9, 0, 3, 0, 7, 0, 0],
[0, 0, 0, 0, 0, 7, 0, 4, 8],
[8, 0, 0, 4, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 9, 8, 0, 0, 3],
[0, 9, 5, 0, 0, 3, 0, 0, 0],
]
test3 = [
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " "],
]
print(fullboard_isvalid(test_validboard))
print(fullboard_isvalid(test_invalidboard))
print(fullboard_isvalid(test3))
print(test3)