2019-11-30 13:55:22 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
"""This is a Sudoku Solver."""
|
|
|
|
|
|
|
|
from board import Board
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-11-30 17:04:43 +01:00
|
|
|
"""Provided a string representing a Sudoku game, solve it."""
|
|
|
|
|
2019-11-30 13:55:22 +01:00
|
|
|
string = (
|
|
|
|
"..4...618\n"
|
|
|
|
"9...8....\n"
|
|
|
|
".2...6.4.\n"
|
|
|
|
"...5.34.6\n"
|
|
|
|
"7.3.1.2.5\n"
|
|
|
|
"6.94.7...\n"
|
|
|
|
".6.2...8.\n"
|
|
|
|
"....4...3\n"
|
|
|
|
"418...7..\n"
|
|
|
|
)
|
|
|
|
|
2019-11-30 17:52:45 +01:00
|
|
|
while '.' in string:
|
|
|
|
new_string = iterate_board(string)
|
2019-11-30 17:54:07 +01:00
|
|
|
assert new_string != string
|
2019-11-30 17:52:45 +01:00
|
|
|
string = new_string
|
|
|
|
|
|
|
|
|
|
|
|
def iterate_board(string):
|
|
|
|
"""This function consists of one iteration through the solving process.
|
2019-11-30 13:55:22 +01:00
|
|
|
|
2019-11-30 17:52:45 +01:00
|
|
|
Return an updated string.
|
|
|
|
"""
|
2019-11-30 13:55:22 +01:00
|
|
|
|
2019-11-30 17:52:45 +01:00
|
|
|
board = Board(string)
|
2019-11-30 13:55:22 +01:00
|
|
|
board.compute_possible_values()
|
2019-11-30 17:04:43 +01:00
|
|
|
board.intersect_lines_and_columns()
|
2019-11-30 13:55:22 +01:00
|
|
|
board.create_squares()
|
2019-11-30 17:04:43 +01:00
|
|
|
board.discard_with_grids()
|
2019-11-30 17:52:45 +01:00
|
|
|
print(f"New board:\n{board}")
|
2019-11-30 17:04:43 +01:00
|
|
|
|
2019-11-30 17:52:45 +01:00
|
|
|
return board.get_new_board()
|
2019-11-30 15:21:33 +01:00
|
|
|
|
2019-11-30 13:55:22 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|