Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30732c2

Browse files
authored
Merge pull request metafy-social#353 from YashIndane/master
Added Sudoku Solver
2 parents 9a423f1 + 7acfa5e commit 30732c2

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

scripts/Sudoku-Solver/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is a Sudoku Solver that uses backtracking algorithm to solve the puzzle.
2+
Input the puzzle from command line.

scripts/Sudoku-Solver/main.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import numpy as np
2+
3+
problem = []
4+
5+
for x in range(9):
6+
i = input()
7+
l = [int(v) for v in i]
8+
problem.append(l)
9+
10+
#print(problem)
11+
12+
np_problem = np.array(problem)
13+
14+
fixed_coordinates = [] # first getting the coordinates where fixed numbers are present
15+
empty_coordinates = []
16+
for i , sub_array in enumerate(problem) :
17+
temp = [[i , c] for c , sub_element in enumerate(sub_array) if sub_element > 0]
18+
temp2 = [[i , j] for j , sub_element2 in enumerate(sub_array) if sub_element2 == 0]
19+
for z in temp : fixed_coordinates.append(z)
20+
for w in temp2 : empty_coordinates.append(w)
21+
22+
l , m , r = [0 , 3 , 6] , [1 , 4 , 7] , [2 , 5 , 8]
23+
24+
avoid_dict = {idx : [] for idx in list(range(0 , len(empty_coordinates)))}
25+
26+
def generate_bounds(r , c) -> list:
27+
28+
lower_bound_c = c if c in l else c - 1 if c in m else c - 2
29+
upper_bound_c = c + 3 if c in l else c + 2 if c in m else c + 1
30+
31+
lower_bound_r = r if r in l else r - 1 if r in m else r - 2
32+
upper_bound_r = r + 3 if r in l else r + 2 if r in m else r + 1
33+
34+
return [lower_bound_c , upper_bound_c , lower_bound_r , upper_bound_r]
35+
36+
37+
def backtrack(return_coordinates) :
38+
39+
n_r , n_c = empty_coordinates[empty_coordinates.index(return_coordinates) - 1] # getting back element coordinates
40+
41+
while [n_r , n_c] != empty_coordinates[empty_coordinates.index(return_coordinates) + 1]:
42+
43+
if np_problem[n_r , n_c] != 0 :
44+
avoid_dict[empty_coordinates.index([n_r , n_c])].append(np_problem[n_r , n_c])
45+
46+
fix_flag = False
47+
r , c = n_r , n_c
48+
for num in range(1 , 10) :
49+
50+
l_b_c , u_b_c , l_b_r , u_b_r = generate_bounds(r , c)
51+
52+
if all([num not in np_problem[l_b_r : u_b_r , l_b_c : u_b_c] , num not in np_problem[r , :] , num not in np_problem[: , c]]) :
53+
if num not in avoid_dict.get(empty_coordinates.index([n_r , n_c])) :
54+
np_problem[n_r , n_c] , fix_flag = num , True
55+
break
56+
57+
if fix_flag : n_r , n_c = empty_coordinates[empty_coordinates.index([n_r , n_c]) + 1]
58+
59+
if not fix_flag :
60+
np_problem[n_r , n_c] = 0
61+
avoid_dict[empty_coordinates.index([n_r , n_c])].clear()
62+
n_r , n_c = empty_coordinates[empty_coordinates.index([n_r , n_c]) - 1]
63+
64+
65+
for r in range(9) :
66+
for c in range(9) :
67+
68+
if [r , c] not in fixed_coordinates :
69+
70+
fix_flag = False
71+
72+
for num in range(1 , 10) :
73+
74+
l_b_c , u_b_c , l_b_r , u_b_r = generate_bounds(r , c)
75+
76+
if all([num not in np_problem[l_b_r : u_b_r , l_b_c : u_b_c] , num not in np_problem[r , :] , num not in np_problem[: , c]]) :
77+
78+
np_problem[r , c] , fix_flag = num , True
79+
break
80+
81+
if not fix_flag : backtrack([r , c])
82+
83+
print(np_problem)

0 commit comments

Comments
 (0)