Created
January 5, 2019 06:45
-
-
Save zahash/5edda2b3ba6b5d615a5e94d3b934718c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start = '53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79' | |
rows = 'ABCDEFGHI' | |
cols = '123456789' | |
boxes = cartesian_product(rows, cols) | |
row_units = [cartesian_product(r, cols) for r in rows] | |
col_units = [cartesian_product(rows, c) for c in cols] | |
box_units = [cartesian_product(r,c) | |
for r in ['ABC', 'DEF', 'GHI'] | |
for c in ['123','456','789']] | |
unit_list = row_units + col_units + box_units | |
# each box(key) with its units(value) | |
unit_dict = dict((box, [unit for unit in unit_list if box in unit]) for box in boxes) | |
# each box with its peers | |
peer_dict = dict((box, set(sum(unit_dict[box], [])) - set([box])) for box in boxes) | |
# start string converted to dictionary | |
assert len(start) == 81 | |
grid_dict = dict(zip(boxes, start)) | |
# display unsolved board | |
display(grid_dict) | |
print('\n'*2) | |
# replacing the dots(.) with '123456789' (possible values in the box) | |
for k,v in grid_dict.items(): | |
if v == '.': | |
grid_dict[k] = '123456789' | |
# ======================================================================= | |
# Do all the testing and solving below this line | |
# ======================================================================= | |
solved_grid = search(grid_dict) | |
# solved_grid = eliminate(grid_dict) | |
display(solved_grid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment