Add obligated algo for columns, still missing something

This commit is contained in:
flying-scorpio 2019-12-01 20:11:17 +01:00
parent 03b5a1a232
commit 0715537c09
2 changed files with 56 additions and 16 deletions

View file

@ -149,31 +149,68 @@ class Board:
self.__compute_possible_values_from_columns() self.__compute_possible_values_from_columns()
def compute_obligated_in_each_line(self): 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 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 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. 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 line_id, line in enumerate(self.lines):
for square_index, square in enumerate(line): 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. # Create an empty set to store the other square's values.
other_values = set() other_values = set()
for other_square_index in range(9): for other_square_id in range(9):
if other_square_index != square_index: # ignore current square
if other_square_id != square_id:
# update other_values with values of other squares
other_values = other_values.union( 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): for number in range(1, 10):
if str(number) in square.values and number not in other_values: if str(number) in square.values and \
self.__debug(number) str(number) not in other_values:
self.lines[line_index][square_index].values = {str(number)} 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): def create_squares(self):
"""Make each square of the board a Square object.""" """Make each square of the board a Square object."""
@ -194,14 +231,15 @@ class Board:
def discard_with_grids(self): def discard_with_grids(self):
"""Remove impossible values in squares, by discarding from grids.""" """Remove impossible values in squares, by discarding from grids."""
for square in self.squares: self.__debug("discard_with_grids")
self.__debug(square.values) for square_index, square in enumerate(self.squares):
self.__debug(self.grids[square.grid_id]) 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: if len(square.values) > 1:
for value in self.grids[square.grid_id]: for value in self.grids[square.grid_id]:
if value in square.values: if value in square.values:
square.values.discard(value) square.values.discard(value)
self.__debug(square.values) self.__debug(f" square n°{square_index}: {square.values}")
def intersect_lines_and_columns(self): def intersect_lines_and_columns(self):
"""Remove impossible values from possibilities in lines and columns. """Remove impossible values from possibilities in lines and columns.

View file

@ -70,7 +70,7 @@ def main():
"47..5....\n" "47..5....\n"
)] )]
for string in strings: for string in strings[2:-3]:
iterations = 0 iterations = 0
while '.' in string: while '.' in string:
iterations += 1 iterations += 1
@ -87,11 +87,13 @@ def iterate_board(string):
""" """
board = Board(string, debug=False) board = Board(string, debug=False)
print(f"New board:\n{board}")
board.compute_possible_values() board.compute_possible_values()
board.intersect_lines_and_columns() board.intersect_lines_and_columns()
board.create_squares() board.create_squares()
board.discard_with_grids() board.discard_with_grids()
board.compute_obligated_in_each_line() board.compute_obligated_in_each_line()
board.compute_obligated_in_each_column()
print(f"New board:\n{board}") print(f"New board:\n{board}")
return board.get_new_board() return board.get_new_board()