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-12-02 15:21:49 +01:00
|
|
|
string = (
|
2019-12-01 19:27:38 +01:00
|
|
|
".....6...\n"
|
|
|
|
"..2.35.8.\n"
|
|
|
|
"....2..13\n"
|
|
|
|
".2....19.\n"
|
|
|
|
"93....7..\n"
|
|
|
|
"5....4...\n"
|
|
|
|
"1......7.\n"
|
|
|
|
".7...142.\n"
|
|
|
|
".9.4.3...\n"
|
2019-12-02 15:21:49 +01:00
|
|
|
)
|
2019-12-01 19:27:38 +01:00
|
|
|
|
2019-12-02 15:21:49 +01:00
|
|
|
iterations = 0
|
|
|
|
while '.' in string:
|
|
|
|
iterations += 1
|
|
|
|
new_string = iterate_board(string, debug=True)
|
|
|
|
assert new_string != string
|
|
|
|
string = new_string
|
|
|
|
print(f"Solved in {iterations} iterations.")
|
2019-11-30 17:52:45 +01:00
|
|
|
|
|
|
|
|
2019-12-02 15:21:49 +01:00
|
|
|
def iterate_board(string, debug=False):
|
2019-11-30 17:52:45 +01:00
|
|
|
"""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-12-02 15:21:49 +01:00
|
|
|
board = Board(string, debug=debug)
|
2019-12-03 12:00:32 +01:00
|
|
|
iterations = 0
|
|
|
|
while '.' in string:
|
|
|
|
iterations += 1
|
|
|
|
print(board)
|
|
|
|
board.compute_possible_values()
|
|
|
|
board.compute_obligated_in_each_line()
|
|
|
|
board.compute_obligated_in_each_column()
|
|
|
|
print(board)
|
|
|
|
board.update_board()
|
2019-11-30 15:21:33 +01:00
|
|
|
|
2019-11-30 13:55:22 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|