-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.py
More file actions
93 lines (75 loc) · 2.55 KB
/
sudoku.py
File metadata and controls
93 lines (75 loc) · 2.55 KB
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
import os
import sys
import time
import pandas as pd
backup = sys.stdout
if os.path.exists('files\\mytimes.txt'):
os.remove('files\\mytimes.txt')
os.remove('files\\solutions.csv')
class PySudoku:
def __init__(self, nums: str, fname='files\solutions.csv', dims=None, matrix=None):
self.fname = fname
myGrid = []
dims = int(len(nums) ** (1 / 2))
nums = str(nums)
while nums:
row = []
myGrid.append(list(map(int, list(nums[:dims]))))
nums = nums[dims:]
self.grid = myGrid
self.dims = dims
@property
def dimensions(self):
return len(self.grid)
def possible(self, y, x, n, dimensions):
for i in range(dimensions):
if self.grid[y][i] == n:
return False
for i in range(dimensions):
if self.grid[i][x] == n:
return False
x0 = (x // int(dimensions ** (1 / 2))) * int(dimensions ** (1 / 2))
y0 = (y // int(dimensions ** (1 / 2))) * int(dimensions ** (1 / 2))
for i in range(int(dimensions ** (1 / 2))):
for j in range(int(dimensions ** (1 / 2))):
if self.grid[y0 + i][x0 + j] == n:
return False
return True
def solver(self):
for y in range(self.dims):
for x in range(self.dims):
if self.grid[y][x] == 0:
for n in range(1, self.dims + 1):
if self.possible(y, x, n, self.dims):
self.grid[y][x] = n
self.solver()
self.grid[y][x] = 0
return
sys.stdout = open(self.fname, 'a')
print(matrix_to_str(self.grid))
def matrix_to_str(matrix):
result = ""
for row in matrix:
for num in row:
result += str(num)
return result
my_csv = pd.read_csv('files\sudoku.csv')
# curr = time.time_ns()
times = {}
def run(start, end, score):
for i in (my_csv['head'].loc[start: end]):
time.sleep(1)
score += 1
curr = time.time()
sudoku = PySudoku(i)
sudoku.solver()
sys.stdout = backup
end = time.time()
solve_time = (round((end - curr) * 1000, 3))
times_file = open('files\\mytimes.txt', 'a+')
print(f'{score},{solve_time}', file=times_file)
times_file.close()
return score
myscore = run(0, 10, 0)
print('Go')
run(10, 100, myscore - 1)