Example #1
0
def main():
    initial_board_values = [
        [0, 9, 6, 0, 4, 0, 0, 3, 0],
        [0, 5, 7, 8, 2, 0, 0, 0, 0],
        [1, 0, 0, 9, 0, 0, 5, 0, 0],
        [0, 0, 9, 0, 1, 0, 0, 0, 8],
        [5, 0, 0, 0, 0, 0, 0, 0, 2],
        [4, 0, 0, 0, 9, 0, 6, 0, 0],
        [0, 0, 4, 0, 0, 3, 0, 0, 1],
        [0, 0, 0, 0, 7, 9, 2, 6, 0],
        [0, 2, 0, 0, 5, 0, 9, 8, 0],
    ]
    board = Board(initial_board_values)
    sudoku_solver = SudokuSolver()
    sudoku_solver.prepare(board)

    start_time = time.time()

    sudoku_solver.run()

    elapsed_time = time.time() - start_time

    result = sudoku_solver.get_result()

    print('Puzzle solved?:   {}'.format(result.success))
    print('Elapsed time (s): {}'.format(elapsed_time))
    if result.success:
        print('\nInitial board:\n')
        print(board)
        board.update_units_by_name(result.assignment.state)
        print('\nSolved board:\n')
        print(board)
    def test_easy_sudoku_puzzle_success(self):
        # Arrange
        expected_board_values = [
            [2, 9, 6, 1, 4, 5, 8, 3, 7],
            [3, 5, 7, 8, 2, 6, 1, 4, 9],
            [1, 4, 8, 9, 3, 7, 5, 2, 6],
            [6, 3, 9, 5, 1, 2, 4, 7, 8],
            [5, 8, 1, 7, 6, 4, 3, 9, 2],
            [4, 7, 2, 3, 9, 8, 6, 1, 5],
            [9, 6, 4, 2, 8, 3, 7, 5, 1],
            [8, 1, 5, 4, 7, 9, 2, 6, 3],
            [7, 2, 3, 6, 5, 1, 9, 8, 4],
        ]
        initial_board_values = [
            [0, 0, 6, 1, 4, 5, 8, 3, 7],
            [3, 5, 7, 8, 2, 6, 1, 4, 9],
            [1, 4, 8, 9, 3, 0, 0, 2, 6],
            [6, 3, 9, 5, 1, 2, 4, 7, 8],
            [5, 0, 1, 7, 6, 0, 3, 0, 2],
            [4, 7, 2, 3, 0, 8, 6, 1, 5],
            [9, 6, 4, 2, 8, 3, 7, 5, 1],
            [8, 1, 0, 4, 7, 9, 2, 6, 3],
            [7, 2, 0, 6, 5, 1, 9, 8, 0],
        ]
        board = Board(initial_board_values)
        sudoku_solver = SudokuSolver()
        sudoku_solver.prepare(board)

        # Act
        sudoku_solver.run()

        # Assert
        result = sudoku_solver.get_result()
        self.assertTrue(result.success)
        board.update_units_by_name(result.assignment.state)
        for i, row in enumerate(board.rows()):
            row_values = [unit.value for unit in row]
            self.assertEqual(expected_board_values[i], row_values)
    def test_impossible_sudoku_puzzle_failure(self):
        # Arrange
        initial_board_values = [
            [6, 6, 1, 1, 1, 5, 8, 3, 7],
            [3, 5, 7, 8, 2, 6, 1, 4, 9],
            [1, 4, 8, 9, 3, 0, 0, 2, 6],
            [6, 3, 9, 5, 1, 2, 4, 7, 8],
            [5, 0, 1, 7, 6, 0, 3, 0, 2],
            [4, 7, 2, 3, 0, 8, 6, 1, 5],
            [9, 6, 4, 2, 8, 3, 7, 5, 1],
            [8, 1, 0, 4, 7, 9, 2, 6, 3],
            [7, 2, 0, 6, 5, 1, 9, 8, 0],
        ]
        board = Board(initial_board_values)
        sudoku_solver = SudokuSolver()
        sudoku_solver.prepare(board)

        # Act
        sudoku_solver.run()

        # Assert
        result = sudoku_solver.get_result()
        self.assertFalse(result.success)
Example #4
0
from set_logger import create_logger
import traceback
import pathlib
from sudoku_solver import SudokuSolver

path = pathlib.Path(__file__).parent.parent.absolute()
logger = create_logger(f'{path}/results', 'INFO')

dataset = 'head_sudoku.csv'

if __name__ == '__main__':
    sudoku_solver = SudokuSolver(path=f'{path}/data', dataset=dataset)
    logger.info(f"Program starts here")
    try:
        sudoku_solver.run()
    except Exception as e:
        track = traceback.format_exc()
        logger.error(
            f"There was an error while executing the promgram {track}")
    logger.info('Ends program')