-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy.py
executable file
·216 lines (181 loc) · 7.02 KB
/
easy.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python
# sudoku solver
from prettytable import *
# initialise sudoku puzzle
rows = {}
rows[0] = [None, None, 2, None, 7, 5, None, 1, 3]
rows[1] = [None, None, None, None, None, None, 4, None, 9]
rows[2] = [7, None, 9, None, 4, None, 8, 2, None]
rows[3] = [3, None, 1, None, 9, 6, None, None, None]
rows[4] = [6, None, None, 3, None, 7, None, None, 1]
rows[5] = [None, None, None, 5, 8, None, 3, None, 6]
rows[6] = [None, 6, 4, None, 3, None, 5, None, 8]
rows[7] = [2, None, 5, None, None, None, None, None, None]
rows[8] = [8, 9, None, 1, 5, None, 7, None, None]
# need a way of getting columns
def getColumns():
columns = {}
for k in range(0, 9):
columns[k] = []
for i in range(0, 9):
for j in range(0, 9):
columns[i].append(rows[j][i])
return columns
columns = getColumns()
# need a way of getting blocks
def getBlocks():
# initialise blocks dictionary
blocks = {}
for i in range(0, 9):
blocks[i] = []
for k in [0, 3, 6]:
j = 0
for i in [0, 3, 6]:
blocks[k + j] += rows[k][i:i + 3] + \
rows[k + 1][i:i + 3] + rows[k + 2][i:i + 3]
j = j + 1
return blocks
blocks = getBlocks()
# now for actually solving the puzzle
# check which numbers we need to find in a row
def findMissingRowIndexes(row_num):
absent_index = []
count = 0
for item in rows[row_num]:
if item == None:
absent_index.append(count)
count += 1
return absent_index
def possibleNumsRow(row_num):
absent_nums = []
possible_nums = {}
# find absent numbers in row
for number in range(1, 10):
if number not in rows[row_num]:
absent_nums.append(number)
# get indexes of absent numbers
absent_index = findMissingRowIndexes(row_num)
blocks = getBlocks() # reset blocks
for missingIndex in absent_index:
# find which block the number is in
if row_num < 3:
# first three blocks
if missingIndex < 3:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[0]]
elif missingIndex < 6:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[1]]
elif missingIndex < 9:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[2]]
elif row_num < 6:
# second row of blocks
if missingIndex < 3:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[3]]
elif missingIndex < 6:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[4]]
elif missingIndex < 9:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[5]]
elif row_num < 9:
# third row of blocks
if missingIndex < 3:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[6]]
elif missingIndex < 6:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[7]]
elif missingIndex < 9:
possible_nums[missingIndex] = [
x for x in absent_nums if x not in blocks[8]]
# at this point we have possible numbers when taking into account blocks
# now we need to take columns into account
# each key of the possible_nums dictionary is a column nunber
# we can remove any values that already exist in that column
columns = getColumns() # reset columns for when function is used again
for column, possibles in possible_nums.items():
possible_nums[column] = [
x for x in possibles if x not in columns[column]]
return possible_nums
def blockIndex2Row(block, index):
row_num = 0 # reset
for i in [0, 3, 6]:
if block in [i, i + 1, i + 2]:
for j in [0, 3, 6]:
if index in [j, j + 1, j + 2]:
return row_num # we have found the row
else:
row_num += 1 # not this row, try next row
else:
row_num += 3
def enterAnswer():
# enter values into rows
# if only 1 possible in row, fill in
for row in range(0, 9):
for column, possibles in possibleNumsRow(row).items():
if len(possibles) == 1:
rows[row][column] = possibles[0]
# fill in columns with only one left
columns = getColumns()
for column in range(0, 9):
if columns[column].count(None) == 1:
index = columns[column].index(None) # also the row number
absent_num = [x for x in range(1, 10) if x not in columns[column]]
rows[index][column] = absent_num[0]
# reset blocks with new rows
blocks = getBlocks()
# if block has 1 missing value, fill it in
for block in range(0, 9):
if blocks[block].count(None) == 1:
index = blocks[block].index(None)
absent_num = [x for x in range(1, 10) if x not in blocks[block]]
# at this point the block has been filled, but we still need to update the value in rows
# otherwise getBlocks() will reset to the original blocks
# we know the index of the None and can translate this to row/column
row_num = blockIndex2Row(block, index)
# let's update the row with the new value
# there could be multiple None values per row, so we must be careful
# we have to make sure the None is the one inside this block when we replace it
# let's get the indexes of all the None values
none_index = [i for i, j in enumerate(rows[row_num]) if j == None]
if block in [0, 3, 6]:
none_index = [x for x in none_index if x < 3]
if block in [1, 4, 7]:
none_index = [x for x in none_index if x > 2 and x < 6]
if block in [2, 5, 8]:
none_index = [x for x in none_index if x > 5 and x < 9]
rows[row_num][none_index[0]] = absent_num[0]
return 0
def noneCount():
# counts how many empty squares are left
count = 0
for i in range(0, 9):
for item in rows[i]:
if item == None:
count += 1
return count
def prettyPrint():
columns = getColumns()
table = PrettyTable()
table.hrules = ALL
table.add_column(" ", range(0, 9))
for i in [str(x) for x in range(0, 9)]:
table.add_column(i, columns[int(i)])
return table
def results():
# print "ORIGINAL"
# print prettyPrint()
rows_before = 0 # initialise with different values,
rows_after = 1 # doesn't matter what they are
while(rows_before != rows_after):
rows_before = noneCount()
enterAnswer()
rows_after = noneCount()
print "FINAL"
print prettyPrint()
results()
for i in range(0, 9):
print i, possibleNumsRow(i)