Sudoku solver
This is a sudoku solver that helps you solve sudoku puzzles by showing you the easiest possible strategy required to solve the puzzle.
If you're stuck on a sudoku puzzle, you can use this library to find out if you missed something obvious, or need to apply a more complex strategy to make progress.
See also my blogpost Introducing an actually helpful sudoku solver.
Setup
Install the package:
pip install sudoku-solver-tim
Usage
from sudoku_solver_tim import Puzzle
grid = [
[0, 0, 1, 9, 5, 7, 0, 6, 3],
[0, 0, 0, 8, 0, 6, 0, 7, 0],
[7, 6, 9, 1, 3, 0, 8, 0, 5],
[0, 0, 7, 2, 6, 1, 3, 5, 0],
[3, 1, 2, 4, 9, 5, 7, 8, 6],
[0, 5, 6, 3, 7, 8, 0, 0, 0],
[1, 0, 8, 6, 0, 9, 5, 0, 7],
[0, 9, 0, 7, 1, 0, 6, 0, 8],
[6, 7, 4, 5, 8, 3, 0, 0, 0],
]
puzzle = Puzzle(grid)
# Find the easiest strategy to make progress (remove a pencil mark)
puzzle.solve_step()
#> Made progress using candidate_lines
# Solve the puzzle using all strategies
puzzle.solve()
puzzle.strategies_used
# {'Candidate Lines', 'Single Candidate'}
puzzle
#> ┏━━━┯━━━┯━━━┓ ┏━━━┯━━━┯━━━┓ ┏━━━┯━━━┯━━━┓
#> ┃ 2 │ 8 │ 1 ┃ ┃ 9 │ 5 │ 7 ┃ ┃ 4 │ 6 │ 3 ┃
#> ┠───┼───┼───┨ ┠───┼───┼───┨ ┠───┼───┼───┨
#> ┃ 4 │ 3 │ 5 ┃ ┃ 8 │ 2 │ 6 ┃ ┃ 9 │ 7 │ 1 ┃
#> ┠───┼───┼───┨ ┠───┼───┼───┨ ┠───┼───┼───┨
#> ┃ 7 │ 6 │ 9 ┃ ┃ 1 │ 3 │ 4 ┃ ┃ 8 │ 2 │ 5 ┃
#> ┗━━━┷━━━┷━━━┛ ┗━━━┷━━━┷━━━┛ ┗━━━┷━━━┷━━━┛
#> ┏━━━┯━━━┯━━━┓ ┏━━━┯━━━┯━━━┓ ┏━━━┯━━━┯━━━┓
#> ┃ 8 │ 4 │ 7 ┃ ┃ 2 │ 6 │ 1 ┃ ┃ 3 │ 5 │ 9 ┃
#> ┠───┼───┼───┨ ┠───┼───┼───┨ ┠───┼───┼───┨
#> ┃ 3 │ 1 │ 2 ┃ ┃ 4 │ 9 │ 5 ┃ ┃ 7 │ 8 │ 6 ┃
#> ┠───┼───┼───┨ ┠───┼───┼───┨ ┠───┼───┼───┨
#> ┃ 9 │ 5 │ 6 ┃ ┃ 3 │ 7 │ 8 ┃ ┃ 2 │ 1 │ 4 ┃
#> ┗━━━┷━━━┷━━━┛ ┗━━━┷━━━┷━━━┛ ┗━━━┷━━━┷━━━┛
#> ┏━━━┯━━━┯━━━┓ ┏━━━┯━━━┯━━━┓ ┏━━━┯━━━┯━━━┓
#> ┃ 1 │ 2 │ 8 ┃ ┃ 6 │ 4 │ 9 ┃ ┃ 5 │ 3 │ 7 ┃
#> ┠───┼───┼───┨ ┠───┼───┼───┨ ┠───┼───┼───┨
#> ┃ 5 │ 9 │ 3 ┃ ┃ 7 │ 1 │ 2 ┃ ┃ 6 │ 4 │ 8 ┃
#> ┠───┼───┼───┨ ┠───┼───┼───┨ ┠───┼───┼───┨
#> ┃ 6 │ 7 │ 4 ┃ ┃ 5 │ 8 │ 3 ┃ ┃ 1 │ 9 │ 2 ┃
#> ┗━━━┷━━━┷━━━┛ ┗━━━┷━━━┷━━━┛ ┗━━━┷━━━┷━━━┛
You can also create a puzzle from a string:
string = "2.48........7.5....13.....9..7.......26....3.3...26.4...9..845.87.....16....6.2.."
puzzle = Puzzle.from_string(string)
puzzle.solve()
Techniques implemented
The following techniques are implemented, in order of complexity:
Easy:
Medium:
Advanced:
Master:
- X-Wings
- Swordfish
brute_force(also known as "backtracking"). It will try all possible combinations and backtrack if there is a mistake. You could see this as a variant on the techniques Forcing Chains, Nishio and Guessing.
Some remarks:
- We have not implemented Y-wings, although you do not need them given the other strategies.
- The implementation of
swordfishincluded both Swordfish-3 and Swordfish-4. Swordfish-4 is sometimes called "Jellyfish", and could be a separate strategy. - Forcing Chains is not guesswork/brute force, but it's a lot of hard work if you had to do it by hand.
Metadata
Release files for sudoku-solver-tim 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sudoku_solver_tim-0.1.1.tar.gz | 154.1 kB | With provenance | Details for sudoku_solver_tim-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sudoku_solver_tim-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for sudoku_solver_tim-0.1.1-py3-none-any.whl |
Total release size: 191.2 kB
Release files / sudoku_solver_tim-0.1.1.tar.gz
| Download URL | sudoku_solver_tim-0.1.1.tar.gz |
|---|---|
| Size | 154.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6959ea72604dee666de50c121449096c4c221cd1d7b696ed8f39de8e3ef3361d
|
|
BLAKE2b-256 checksum How to use checksums |
aece6f483758e3c44f8dad10dc910175736c79ea6d187736abddee716cd37698
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 21, 2025.
Transparency logRelease files / sudoku_solver_tim-0.1.1-py3-none-any.whl
| Download URL | sudoku_solver_tim-0.1.1-py3-none-any.whl |
|---|---|
| Size | 37.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aaa9e8fd296630286fe14dc75feca7b5945e2fde4278b77468a240fed87997f7
|
|
BLAKE2b-256 checksum How to use checksums |
ecb88ca53ec290e55fda91858b18571b7fec15f08c64460a5530e3288f8417dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 21, 2025.
Transparency logSecurity for sudoku-solver-tim 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for sudoku_solver_tim-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 21, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.