-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_solver.py
More file actions
75 lines (60 loc) · 2.09 KB
/
simple_solver.py
File metadata and controls
75 lines (60 loc) · 2.09 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
"""
Solves sudokus
:author: kvan
"""
import pprint
import numpy as np
SUDOKU_SIZE = 9
def is_valid_set(arr: np.ndarray) -> bool:
no_zeros = arr[arr != 0]
return len(no_zeros) == len(set(no_zeros))
def is_valid(board: np.ndarray) -> bool:
for row in board:
if not is_valid_set(row):
return False
for col in enumerate(board):
if not is_valid_set(board[:, col[0]]):
return False
for grid in enumerate(board):
if not is_valid_set(board[grid[0]:3, grid[0]:3]):
return False
return True
def is_solved(board: np.ndarray) -> tuple:
for row in enumerate(board):
for col in enumerate(row[1]):
if board[row[0]][col[0]] == 0:
return (row[0], col[0])
return (None, None)
def solver(board: np.ndarray) -> tuple:
"""Solves given sudoku board that is passed in
Args:
board (np.ndarray): the board to solve
"""
(row, col) = is_solved(board)
if row is None and col is None:
return (True, board)
for test_number in range(1, len(board)+1):
board[row][col] = test_number
# pprint.pprint(board)
if is_valid(board):
solved, next_board = solver(np.copy(board))
if solved:
return (True, next_board)
# if(is_valid(board) and solver(np.copy(board))[0]):
# return (True, board)
return (False, board)
def solve_sudoku(board: np.ndarray) -> np.ndarray:
_, solved_board = solver(board)
return solved_board
sudoku_board = np.array([[5, 6, 1, 0, 0, 0, 7, 4, 9],
[8, 0, 9, 0, 0, 6, 2, 1, 3],
[0, 0, 0, 0, 9, 0, 6, 0, 0],
[7, 1, 4, 6, 0, 0, 8, 3, 0],
[0, 2, 0, 0, 0, 4, 0, 6, 7],
[6, 3, 8, 7, 0, 1, 5, 9, 4],
[0, 0, 0, 4, 0, 3, 0, 2, 5],
[0, 9, 0, 0, 0, 5, 0, 0, 6],
[4, 0, 7, 0, 0, 2, 0, 0, 0]])
pprint.pprint(sudoku_board)
sudoku_board = solve_sudoku(sudoku_board)
pprint.pprint(sudoku_board)