-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver_class.py
247 lines (190 loc) · 9.33 KB
/
solver_class.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import utils
from strat_handler import StratHandler
import strats
class Solver:
def __init__(self, current_board):
self.solved = current_board.solved
self.unsolved = current_board.unsolved
self.unsolved_units = self.initialize_units()
def update_unsolved(self, current_board):
# In the case a cell was set, first eliminate the keys from the possibles:
current_board.unsolved = {key: value for key, value in current_board.unsolved.items() if
key not in current_board.solved}
for cell in current_board.unsolved.keys():
visible_units = self.get_visible_cells(cell)
visible_cells = set.union(visible_units['row'], visible_units['col'], visible_units['box'])
taken_numbers = utils.get_cells(current_board.solved, *visible_cells)
current_board.unsolved[cell] -= taken_numbers
return current_board.unsolved
@staticmethod
def get_visible_cells(cell):
"""Return a dictionary containing sets of all cells visible to the given cell, categorized by unit."""
row, col = cell
row_units = {(row, i) for i in range(9)}
col_units = {(i, col) for i in range(9)}
# Calculate the box coordinates
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
box_units = {(i, j) for i in range(start_row, start_row + 3) for j in range(start_col, start_col + 3)}
units = {
'row': row_units,
'col': col_units,
'box': box_units,
}
# Exclude the current cell from the units
for unit_cells in units.values():
unit_cells.discard(cell)
return units
def initialize_units(self):
"""Return a dictionary containing sets of all cells in each unit, categorized by unit type and index.
Important note: only the cells that are empty in self.board are included in the units.
example:
units = {
'row': {
0: {(0, 0), (0, 1), (0, 2), ..., (0, 8)},
1: {(1, 0), (1, 1), (1, 2), ..., (1, 8)},
...
8: {(8, 0), (8, 1), (8, 2), ..., (8, 8)}
},
'col': {...},
'box': {...}}
"""
units = {'Arow': {i: set() for i in range(9)},
'Ccol': {i: set() for i in range(9)},
'Bbox': {i: set() for i in range(9)}}
for (row, col) in self.unsolved.keys():
box_index = (row // 3) * 3 + col // 3
units['Arow'][row].add((row, col))
units['Ccol'][col].add((row, col))
units['Bbox'][box_index].add((row, col))
return units
def board_solved(self):
return len(self.solved) == 81
@staticmethod
def remove_candidates(current_board, eliminated_candidates):
for cell, candidate in eliminated_candidates:
current_board.unsolved[cell].discard(candidate)
return current_board.unsolved
# STRATEGIES
# ///////////////////////////////////////////////////////////////
def naked_singles(self):
n_singles, n_singles_text = strats.naked_singles(self.unsolved)
success = bool(n_singles)
result = StratHandler('Naked Singles',
success,
n_singles if success else None,
None,
n_singles if success else None,
n_singles if success else None,
n_singles_text if success else None)
return result
def hidden_singles(self):
h_singles, h_singles_text = strats.hidden_singles(self.unsolved, self.unsolved_units)
success = bool(h_singles)
result = StratHandler('Hidden Singles',
success,
h_singles if success else None,
None,
h_singles if success else None,
h_singles if success else None,
h_singles_text)
return result
def naked_sets(self, set_filter):
for set_size in set_filter:
strat_name = None
match set_size:
case 2:
strat_name = 'Naked Pairs'
case 3:
strat_name = 'Naked Triples'
case 4:
strat_name = 'Naked Quads'
naked_sets = strats.naked_sets_find(self.unsolved,
set_size,
self.unsolved_units)
processed_naked_sets, highlight_list, eliminate_list = strats.naked_sets_process(self.unsolved,
naked_sets)
# Double check if there is something to eliminate
success = bool(processed_naked_sets) and bool(eliminate_list)
if success:
n_pairs_text = strats.naked_sets_text_format(processed_naked_sets,
strat_name)
return StratHandler(strat_name,
success,
None,
None,
highlight_list,
eliminate_list,
n_pairs_text)
def hidden_sets(self, set_filter):
for set_size in set_filter:
strat_name = None
match set_size:
case 2:
strat_name = 'Hidden Pairs'
case 3:
strat_name = 'Hidden Triples'
case 4:
strat_name = 'Hidden Quads'
hidden_sets = strats.hidden_sets_find(self.unsolved, set_size, self.unsolved_units)
highlight_list, eliminate_list = strats.hidden_sets_process(self.unsolved, hidden_sets)
success = bool(hidden_sets) and bool(eliminate_list)
if success:
h_pairs_text = strats.format_hidden_sets_text(hidden_sets,
strat_name)
return StratHandler(strat_name,
success,
None,
None,
highlight_list,
eliminate_list,
h_pairs_text)
def intersection_removal(self, intersection_type):
pointing_pairs, box_reductions = strats.intersections_find(self.unsolved, self.unsolved_units)
if intersection_type == 'Pointing Pairs':
highlight_list, eliminate_list = strats.intersections_process(pointing_pairs)
else:
highlight_list, eliminate_list = strats.intersections_process(box_reductions)
success = bool(eliminate_list)
i_removals_text = None
if success:
i_removals = box_reductions if intersection_type == 'Box-Reduction' else pointing_pairs
i_removals_text = strats.format_intersections_text(i_removals,
intersection_type)
result = StratHandler(intersection_type,
success,
None,
None,
highlight_list if success else None,
eliminate_list if success else None,
i_removals_text)
return result
def x_wing(self):
x_wing_results = strats.x_wing_find(self.unsolved, self.unsolved_units)
success = bool(x_wing_results)
highlight_candidates, eliminated_candidates, highlight_cells, description = None, None, None, None
if success:
highlight_candidates, eliminated_candidates, highlight_cells, description = strats.x_wing_process(
x_wing_results)
result = StratHandler('X-Wing ',
success,
None,
highlight_cells if success else None,
highlight_candidates if success else None,
eliminated_candidates if success else None,
description if success else None)
return result
def swordfish(self):
swordfish_results = strats.swordfish_find(self.unsolved, self.unsolved_units)
success = bool(swordfish_results)
highlight_candidates, eliminated_candidates, highlight_cells, description = None, None, None, None
if success:
highlight_candidates, eliminated_candidates, highlight_cells, description = strats.swordfish_process(
swordfish_results)
result = StratHandler('Swordfish ',
success,
None,
highlight_cells if success else None,
highlight_candidates if success else None,
eliminated_candidates if success else None,
description if success else None)
return result