Fill cells that remain alone after "masking" by other cells. For each digit value, "stamp out" the puzzle to see which regions have empty cells that could potentially still be filled with that value. For regions with single cells left, fill those cells with the value. Known pro
(matrix)
| 178 | |
| 179 | |
| 180 | def try_masking(matrix): |
| 181 | """Fill cells that remain alone after "masking" by other cells. |
| 182 | |
| 183 | For each digit value, "stamp out" the puzzle to see which regions have |
| 184 | empty cells that could potentially still be filled with that value. For |
| 185 | regions with single cells left, fill those cells with the value. |
| 186 | |
| 187 | Known problems: |
| 188 | This function causes a 'Bad call' exception in the profile module |
| 189 | (see http://www.python.org/sf/1117670) in some Python installations. |
| 190 | |
| 191 | """ |
| 192 | result = False |
| 193 | for digit in MASK9: |
| 194 | locations = set(RLOCATIONS) |
| 195 | matrix2 = [list(m) for m in matrix] |
| 196 | for row in NINE: |
| 197 | try: |
| 198 | idx = matrix2[row].index(digit) |
| 199 | except ValueError: |
| 200 | idx = -1 |
| 201 | else: |
| 202 | matrix2[row] = [-1 for col in NINE] |
| 203 | for row2 in NINE: |
| 204 | matrix2[row2][idx] = -1 |
| 205 | locations.discard((row//3, idx//3)) |
| 206 | for rrow, rcol in locations: |
| 207 | rcol3 = 3 * rcol |
| 208 | rcol3p3 = rcol3 + 3 |
| 209 | rrow3 = 3 * rrow |
| 210 | region2 = (matrix2[rrow3+i][rcol3:rcol3p3] for i in THREE) |
| 211 | region2 = [x for x in chain(*region2)] |
| 212 | if region2.count(0) == 1: |
| 213 | idx = region2.index(0) |
| 214 | row = rrow3 + idx // 3 |
| 215 | col = rcol3 + idx % 3 |
| 216 | matrix[row][col] = digit |
| 217 | result = True |
| 218 | return result |
| 219 | |
| 220 | |
| 221 | def hypothesize(matrix, row, col, values, depth): |