Skip to content

Instantly share code, notes, and snippets.

@zahash
Created January 5, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zahash/5edda2b3ba6b5d615a5e94d3b934718c to your computer and use it in GitHub Desktop.
Save zahash/5edda2b3ba6b5d615a5e94d3b934718c to your computer and use it in GitHub Desktop.
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