def row_col_grid_complete(puzzle, type): for i in range(0, 9): type_list = [] if type == "rows": type_list = common.get_row(puzzle, i) elif type == "cols": type_list = common.get_column(puzzle, i) else: type_list = common.get_grid(puzzle, i) if not set(type_list) == set([1, 2, 3, 4, 5, 6, 7, 8, 9]): return False return True
def eliminate_cell_values(puzzle, row_num, col_num): remaining_values = [1, 2, 3, 4, 5, 6, 7, 8, 9] row = common.get_row(puzzle, row_num) remaining_values = elimate_list_values(remaining_values, row) col = common.get_column(puzzle, col_num) remaining_values = elimate_list_values(remaining_values, col) grid_num = common.get_grid_number(puzzle, row_num, col_num) grid = common.get_grid(puzzle, grid_num) remaining_values = elimate_list_values(remaining_values, grid) if len(remaining_values) == 1: return {'values': remaining_values, 'status': True} return {'values': remaining_values, 'status': False}
def solve_with_method_5(puzzle, type): pairs = find_pairs_by_type(puzzle, type) if type == 'rows': remaining_type_list = list(pairs['rows'].keys()) elif type == 'cols': remaining_type_list = list(pairs['cols'].keys()) else: remaining_type_list = list(pairs['grids'].keys()) for x in remaining_type_list: if type == 'rows': type_list = common.get_row(puzzle, x) elif type == 'cols': type_list = common.get_column(puzzle, x) else: type_list = common.get_grid(puzzle, x) empty_cell_pair = common.get_empty_cell_refs(type_list) possible_values = pairs[type][x] logger.debug("{} {} ({}) has a missing value pair ({})".format( type, x, type_list, possible_values)) for value in possible_values: puzzle_copy = copy.deepcopy(puzzle) cell_ref = empty_cell_pair[0] if type == 'rows': r = x c = cell_ref elif type == 'cols': r = cell_ref c = x else: r = common.get_row_number_from_grid(puzzle, x, cell_ref) c = common.get_col_number_from_grid(puzzle, x, cell_ref) logger.info( "Attempting to solve puzzle again by updating Row {}, Col {} with value {}" .format(r, c, value)) puzzle_attempt = update_cell(puzzle_copy, r, c, value) puzzle_attempt = solve_with_methods_1_to_4(puzzle_attempt) if (puzzle_attempt['status']): return {'puzzle': puzzle_attempt['puzzle'], 'status': True} return {'puzzle': puzzle, 'status': False}
def grid_elimination(puzzle, grid_num): grid = common.get_grid(puzzle, grid_num) remaining_values = elimate_list_values([1, 2, 3, 4, 5, 6, 7, 8, 9], grid) for test_val in remaining_values: unsolved_cell_list = [0, 1, 2, 3, 4, 5, 6, 7, 8] # When one value remains, cell is solved. for cell in range(0, 9): # Test if cell can be eliminated, as already contains value if grid[cell] > 0: unsolved_cell_list.remove(cell) continue # Test if cell can be eliminated, due to a row match. row_num = common.get_row_number_from_grid(puzzle, grid_num, cell) row = common.get_row(puzzle, row_num) if test_val in row: unsolved_cell_list.remove(cell) continue # Test if cell can be eliminated, due to a col match. col_num = common.get_col_number_from_grid(puzzle, grid_num, cell) col = common.get_column(puzzle, col_num) if test_val in col: unsolved_cell_list.remove(cell) continue if len(unsolved_cell_list) == 1: logger.debug( "Eliminated all cells in grid {} ({}) for value ({}) to reference ({})" .format(grid_num, grid, test_val, unsolved_cell_list[0])) row_num = common.get_row_number_from_grid(puzzle, grid_num, unsolved_cell_list[0]) col_num = common.get_col_number_from_grid(puzzle, grid_num, unsolved_cell_list[0]) puzzle = update_cell(puzzle, row_num, col_num, test_val) else: logger.debug( "All cells in row {} ({}) could not be eliminated for value ({}). Remaining cell references ({})" .format(grid_num, grid, test_val, unsolved_cell_list)) return puzzle
def find_pairs_by_type(puzzle, type): pairs = {'rows': {}, 'cols': {}, 'grids': {}} for num in range(0, 9): type_list = [] if type == "rows": type_list = common.get_row(puzzle, num) elif type == "cols": type_list = common.get_column(puzzle, num) else: type_list = common.get_grid(puzzle, num) if type_list.count(0) == 2: remaining_pair = elimate_list_values([1, 2, 3, 4, 5, 6, 7, 8, 9], type_list) logger.debug("{} ({}) had 2 cells remaining ({})".format( type, type_list, remaining_pair)) pairs[type][num] = remaining_pair logger.info("Pairs : {}".format(json.dumps(pairs))) return pairs
def test_get_row(sef, test_puzzle): first_row = common.get_row(test_puzzle, 0) assert first_row == [1, 2, 3, 0, 0, 0, 0, 0, 0], "First row did not match expected values." last_row = common.get_row(test_puzzle, 8) assert last_row == [0, 0, 0, 0, 0, 0, 7, 8, 9], "Last row did not match expected values."