sudoku

Sudoku solver

Developer Guide

If you are new to using nbdev here are some useful pointers to get you started.

Install sudoku in Development mode

# make sure sudoku package is installed in development mode
$ pip install -e .

# make changes under nbs/ directory
# ...

# compile to have changes apply to sudoku
$ nbdev_prepare

Usage

Installation

Install latest from the GitHub repository:

$ pip install git+https://github.com/emagri/sudoku.git

or from pypi

$ pip install sudoku-board-solver-em

Documentation

Documentation can be found hosted on this GitHub repository’s pages. Additionally you can find package manager specific guidelines on conda and pypi respectively.

How to use

Instantiate a Board class, passing it a list of 9 rows containing the initial Sudoku puzzle.

board = ['8-----41-', 
         '-6-9-----', 
         '3-2------', 
         '-5---814-', 
         '4----1--3', 
         '-8---326-', 
         '1-9------', 
         '-7-3-----', 
         '2-----85-']

Invoking its method solve, you can get a log of the processing and the final result.

b = Board(board)
b.solve(False)
 |0-----------|1-----------|2-----------|3-----------|4-----------|5-----------|6-----------|7-----------|8-----------|
0| 8          | 9          | 5          |   -2---6---| 3          | 7          | 4          | 1          |   -2---6---|
1| 7          | 6          | 4          | 9          | 1          |   -2--5----|   --3-5----|   -23------| 8          |
2| 3          | 1          | 2          |   ---456-8-|   ---456-8-|   ---456---|   ----567-9|   ------7-9|   ----5---9|
3| 6          | 5          | 3          |   -2----7--|   -2------9| 8          | 1          | 4          |   ------7-9|
4| 4          | 2          | 7          |   ----56---|   ----56--9| 1          |   ----5---9| 8          | 3          |
5| 9          | 8          | 1          |   ---45-7--|   ---45----| 3          | 2          | 6          |   ----5-7--|
6| 1          | 4          | 9          |   -2--56-8-|   -2--56-8-|   -2--56---|   --3---7--|   --3---7--|   -2---6---|
7| 5          | 7          | 8          | 3          |   -2-4-6---|   -2-4-6---|   -----6--9|   -2------9| 1          |
8| 2          | 3          | 6          | 1          | 7          | 9          | 8          | 5          | 4          |
 |0-----------|1-----------|2-----------|3-----------|4-----------|5-----------|6-----------|7-----------|8-----------|
<Solution.NOT_FOUND: 'solution not found'>

In the above output, the puzzle was only solved partially. In non solved cells, on the right are showed the candidate values. The parameter use_brute_force (disabled by default) enables the use of brute force in case attemps based on logic are not enough to solve the puzzle.

b = Board(board)
b.solve(True)
 |0--|1--|2--|3--|4--|5--|6--|7--|8--|
0| 8 | 9 | 5 | 2 | 3 | 7 | 4 | 1 | 6 |
1| 7 | 6 | 4 | 9 | 1 | 5 | 3 | 2 | 8 |
2| 3 | 1 | 2 | 8 | 6 | 4 | 9 | 7 | 5 |
3| 6 | 5 | 3 | 7 | 2 | 8 | 1 | 4 | 9 |
4| 4 | 2 | 7 | 6 | 9 | 1 | 5 | 8 | 3 |
5| 9 | 8 | 1 | 4 | 5 | 3 | 2 | 6 | 7 |
6| 1 | 4 | 9 | 5 | 8 | 6 | 7 | 3 | 2 |
7| 5 | 7 | 8 | 3 | 4 | 2 | 6 | 9 | 1 |
8| 2 | 3 | 6 | 1 | 7 | 9 | 8 | 5 | 4 |
 |0--|1--|2--|3--|4--|5--|6--|7--|8--|
<Solution.ONE_SOLUTION: 'found a solution'>

Look at 02_main for other examples and tests.