From 0715537c095d89fbd63934ae8b46cb756b9ee1c4 Mon Sep 17 00:00:00 2001 From: flying-scorpio Date: Sun, 1 Dec 2019 20:11:17 +0100 Subject: [PATCH] Add obligated algo for columns, still missing something --- board.py | 68 +++++++++++++++++++++++++++++++++++++++++++------------- main.py | 4 +++- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/board.py b/board.py index 276cba3..10c52bc 100644 --- a/board.py +++ b/board.py @@ -149,31 +149,68 @@ class Board: self.__compute_possible_values_from_columns() def compute_obligated_in_each_line(self): - """Find obligated values per for every number. + """Find obligated values per line for every number. Go through each line. For each square, go through numbers 1 to 9. If the number is in the square's values but not in the values of the rest of the squares in the line then the square should take that number. """ - self.__debug("++++++") + self.__debug("compute_obligated_in_each_line") - for line_index, line in enumerate(self.lines): - for square_index, square in enumerate(line): + for line_id, line in enumerate(self.lines): + self.__debug(f"Line n°{line_id}") + for square_id, square in enumerate(line): + self.__debug(f" square n°{square_id}, {square.values}") # Create an empty set to store the other square's values. other_values = set() - for other_square_index in range(9): - if other_square_index != square_index: + for other_square_id in range(9): + # ignore current square + if other_square_id != square_id: + # update other_values with values of other squares other_values = other_values.union( - line[other_square_index].values + line[other_square_id].values ) - self.__debug(other_values) + self.__debug(f" other values: {other_values}") for number in range(1, 10): - if str(number) in square.values and number not in other_values: - self.__debug(number) - self.lines[line_index][square_index].values = {str(number)} + if str(number) in square.values and \ + str(number) not in other_values: + self.__debug(f" {number}") + self.lines[line_id][square_id].values = {str(number)} + + def compute_obligated_in_each_column(self): + """Find obligated values per column for every number. + + Go through each column. For each square, go through numbers 1 to 9. If + the number is in the square's values but not in the values of the rest + of the squares in the column then the square should take that number. + """ + + self.__debug("compute_obligated_in_each_column") + + for col_id, column in enumerate(self.columns): + self.__debug(f"Column n°{col_id}") + for square_id, square in enumerate(column): + self.__debug(f" square n°{square_id}, {square.values}") + # Create an empty set to store the other square's values. + other_values = set() + for other_square_id in range(9): + # ignore current square + if other_square_id != square_id: + # update other_values with values of other squares + other_values = other_values.union( + column[other_square_id].values + ) + + self.__debug(f" other values: {other_values}") + + for number in range(1, 10): + if str(number) in square.values and \ + str(number) not in other_values: + self.__debug(f" {number}") + self.columns[col_id][square_id].values = {str(number)} def create_squares(self): """Make each square of the board a Square object.""" @@ -194,14 +231,15 @@ class Board: def discard_with_grids(self): """Remove impossible values in squares, by discarding from grids.""" - for square in self.squares: - self.__debug(square.values) - self.__debug(self.grids[square.grid_id]) + self.__debug("discard_with_grids") + for square_index, square in enumerate(self.squares): + self.__debug(f" square n°{square_index}: {square.values}") + self.__debug(f" values in grid: {self.grids[square.grid_id]}") if len(square.values) > 1: for value in self.grids[square.grid_id]: if value in square.values: square.values.discard(value) - self.__debug(square.values) + self.__debug(f" square n°{square_index}: {square.values}") def intersect_lines_and_columns(self): """Remove impossible values from possibilities in lines and columns. diff --git a/main.py b/main.py index 63981d7..682d2aa 100644 --- a/main.py +++ b/main.py @@ -70,7 +70,7 @@ def main(): "47..5....\n" )] - for string in strings: + for string in strings[2:-3]: iterations = 0 while '.' in string: iterations += 1 @@ -87,11 +87,13 @@ def iterate_board(string): """ board = Board(string, debug=False) + print(f"New board:\n{board}") board.compute_possible_values() board.intersect_lines_and_columns() board.create_squares() board.discard_with_grids() board.compute_obligated_in_each_line() + board.compute_obligated_in_each_column() print(f"New board:\n{board}") return board.get_new_board()