-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_b.py
85 lines (75 loc) · 2.56 KB
/
sudoku_b.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
'''
Created on Apr 28, 2014
@author: savs95
'''
'''
Part B: Implement the following 2 rules to solve the Sudoku Puzzle.
Rule 1: If a cell can take only one number, assign that number to that cell.
Rule 2: If there is a number which can go into only one cell, then assign that number to that cell.
'''
import sudoku_a
hash1=sudoku_a.form_hash()
possible_hash={}
arr= [[0, 6, 0, 3, 0, 0, 8, 0, 4],
[5, 3, 7, 0, 9, 0, 0, 0, 0],
[0, 4, 0, 0, 0, 6, 3, 0, 7],
[0, 9, 0, 0, 5, 1, 2, 3, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[7, 1, 3, 6, 2, 0, 0, 4, 0],
[3, 0, 6, 4, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 6, 0, 5, 2, 3],
[1, 0, 2, 0, 0, 9, 0, 8, 0]]
'''
arr=[['.', 6, '.', 1, '.', 4, '.', 5, '.'],
['.', '.', 8, 3, '.', 5, 6, '.', '.'],
[2, '.', '.', '.', '.', '.', '.', '.', 1],
[8, '.', '.', 4, '.', 7, '.', '.', 6],
['.', '.', 6, '.', '.', '.', 3, '.', '.'],
[7, '.', '.', 9, '.', 1, '.', '.', 4],
[5, '.', '.', '.', '.', '.', '.', '.', 2],
['.', '.', 7, 2, '.', 6, 9, '.', '.'],
['.', 4, '.', 5, '.', 8, '.', 7, '.']]'''
def rule1(arr):
number=[1,2,3,4,5,6,7,8,9]
hash1=sudoku_a.form_hash()
#print hash1
possible_hash={}
for i in range(9):
for j in range(9):
possible_hash[(i,j)]=[]
neighbours=hash1[(i,j)][:]
temp_list=[]
for elem1 in neighbours:
temp_list.append(arr[elem1[0]][elem1[1]])
for t in number:
if t not in temp_list and arr[i][j]==0:
possible_hash[(i,j)].append(t)
for i in range(9):
for j in range(9):
if len (possible_hash[(i,j)])==1:
arr[i][j]=possible_hash[(i,j)][0]
possible_hash[(i,j)]=[]
rule1(arr)
return possible_hash
def rule2(arr):
rule1(arr)
possible_hash=rule1(arr)
hash1=sudoku_a.form_hash()
for i in range(9):
for j in range(9):
neighbours=hash1[(i,j)][:]
temp_ref=possible_hash[(i,j)][:]
temp_ref_elem=[]
temp_ref_fin=[]
for elem in neighbours:
temp_ref_elem=temp_ref_elem+possible_hash[elem]
for t in temp_ref:
if t not in temp_ref_elem:
temp_ref_fin.append(t)
if (len(temp_ref_fin)==1):
arr[i][j]=temp_ref_fin[0]
rule2(arr)
#example to solve sudoku
rule2(arr)
for i in arr:
print i