This repository has been archived by the owner on Oct 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suduko.py
128 lines (108 loc) · 3.36 KB
/
suduko.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
# Project: Suduko Solver
# Author : Ayush Gupta
# Date created : 24-12-2020
def empty(puzzle):
# arguement: 2d list
# returns: list (only two values of the empty square)
for y in range(9):
for x in range(9):
if puzzle[y][x] == 0 :
return y,x
return None, None
def valid_value(puzzle, y, x, value):
# checks whether the value is valid for that coordinate
# [1] : check the x-axis
for i in range(9):
if puzzle[y][i] == value:
return False
# [2] : check the y-axis
for j in range(9):
if puzzle[j][x] == value:
return False
# [3] : check the local square
x0 = (x//3)*3
y0 = (y//3)*3
for r in range(y0, y0+3):
for c in range(x0, x0+3):
if puzzle[r][c] == value:
return False
return True
def valid_value2(puzzle, y, x, value):
# checks whether the value is valid for that coordinate
# [1] : check the x-axis
for i in range(9):
if i!=x:
if puzzle[y][i] == value:
return False
# [2] : check the y-axis
for j in range(9):
if j!=y:
if puzzle[j][x] == value:
return False
# [3] : check the local square
x0 = (x//3)*3
y0 = (y//3)*3
for r in range(y0, y0+3):
for c in range(x0, x0+3):
if x!=c and y!=r:
if puzzle[r][c] == value:
return False
return True
def solve(puzzle):
y,x=empty(puzzle)
# puzzle is completed if there aren't any boxes left
if y is None:
return True
for guess in range(1,10):
if valid_value(puzzle, y, x, guess):
puzzle[y][x] = guess
if solve(puzzle):
return True
puzzle[y][x] = 0
return False
def solved(puzzle):
answer=""
for i in range(9):
for j in range(9):
if j==3 or j==6:
answer += " |"
answer += " "+str(puzzle[i][j])
answer += "\n"
if i==2 or i==5:
answer += " ---------------------------------\n"
return answer
def print_board(puzzle):
print(solved(puzzle),end="")
# open the text file and read the grid
grid = [[]for i in range(9)]
file = open("suduko.txt", "r")
fileInfo = file.read()
file.close()
def readFileInfo():
ngrid = grid.copy()
j = 0
for num in fileInfo:
if len(ngrid[j])==9:
j+=1
if num.isdigit():
ngrid[j].append(int(num))
elif num == "-":
ngrid[j].append(int(0))
return ngrid
grid = readFileInfo()
if __name__ == "__main__":
if solve(grid):
print("\nSolution:\n")
print_board(grid)
file = open("solution.txt","w")
file.write(str(solved(grid)))
file.close()
print("\nCopied the solution to <solution.txt>")
# testing the solution
for y in range(9):
for x in range(9):
if not valid_value2(grid, y, x, grid[y][x]):
print("\nTHERE IS A PROBLEM, please input a valid grid\n")
break
else:
print("There is a problem")