-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve_multiple.py
71 lines (56 loc) · 2.33 KB
/
solve_multiple.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
import numpy as np
from alive_progress import alive_bar
from time import process_time, perf_counter
from test_scripts.tests import test_sudoku
from test_scripts.utils import save_sudoku
def solve_multiple(sudoku_array: np.ndarray,
save_to_file: str or None,
use_process_time: bool,
quit_after: int) -> np.ndarray or None:
'''
Solves multiple sudokus and prints the results.
@args
sudoku_array (np.ndarray) : A 3D array of the sudokus to solve
save_to_file (bool) : Whether or not to save the variables
(Can increase processing time)
use_process_time (bool) : Whether or not to use time.process_time()
instead of time.perf_counter()
quit_after (int) : The number of sudokus to stop processing after.
'''
total_time = 0
total_count = len(sudoku_array) if quit_after == -1 else quit_after
fastest = 10000
slowest = 0
solutions = np.ndarray
script_start_time = perf_counter()
array_to_solve = sudoku_array if quit_after == -1 else sudoku_array[:quit_after]
count = 0
try:
with alive_bar(total_count) as bar:
for sudoku in array_to_solve:
your_solution, time_taken = test_sudoku(sudoku, use_process_time)
total_time += time_taken
if time_taken < fastest:
fastest = time_taken
if time_taken > slowest:
slowest = time_taken
if save_to_file:
solutions = np.append(solutions, solution)
count += 1
bar()
except KeyboardInterrupt:
pass
script_end_time = perf_counter()
print( f'''
# - - - - - - - - - - - - - - - - - - - - - - - #
Solved {count} sudokus in {round(script_end_time - script_start_time, 3)} seconds.
TOTAL TIME {total_time / 1000} s
SUDOKUS SOLVED {count}
FASTEST TIME {fastest} ms
SLOWEST TIME {slowest} ms
AVERAGE TIME {total_time / count} ms
Calculated using time.{"process_time" if use_process_time else "perf_counter"}()
# - - - - - - - - - - - - - - - - - - - - - - - #
''')
if save_to_file:
save_sudoku(solutions, save_to_file)