Sudoku solver that will use successively more advanced techniques to solve the puzzle.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sudoku_solver_tim-0.1.1.tar.gz.
File metadata
- Download URL: sudoku_solver_tim-0.1.1.tar.gz
- Upload date:
- Size: 154.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6959ea72604dee666de50c121449096c4c221cd1d7b696ed8f39de8e3ef3361d
|
|
| MD5 |
a53c94aa3bbbc38a61d5b8e5551a210d
|
|
| BLAKE2b-256 |
aece6f483758e3c44f8dad10dc910175736c79ea6d187736abddee716cd37698
|
Provenance
The following attestation bundles were made for sudoku_solver_tim-0.1.1.tar.gz:
Publisher:
publish_to_pypi.yml on timvink/sudoku-solver
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sudoku_solver_tim-0.1.1.tar.gz -
Subject digest:
6959ea72604dee666de50c121449096c4c221cd1d7b696ed8f39de8e3ef3361d - Sigstore transparency entry: 186068418
- Sigstore integration time:
-
Permalink:
timvink/sudoku-solver@924f6c61f84d95d9a6d05f46a1ea8af8f84288fa -
Branch / Tag:
refs/heads/main - Owner: https://github.com/timvink
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@924f6c61f84d95d9a6d05f46a1ea8af8f84288fa -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file sudoku_solver_tim-0.1.1-py3-none-any.whl.
File metadata
- Download URL: sudoku_solver_tim-0.1.1-py3-none-any.whl
- Upload date:
- Size: 37.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaa9e8fd296630286fe14dc75feca7b5945e2fde4278b77468a240fed87997f7
|
|
| MD5 |
ad44822f7497fa8e634123a6d0278664
|
|
| BLAKE2b-256 |
ecb88ca53ec290e55fda91858b18571b7fec15f08c64460a5530e3288f8417dd
|
Provenance
The following attestation bundles were made for sudoku_solver_tim-0.1.1-py3-none-any.whl:
Publisher:
publish_to_pypi.yml on timvink/sudoku-solver
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sudoku_solver_tim-0.1.1-py3-none-any.whl -
Subject digest:
aaa9e8fd296630286fe14dc75feca7b5945e2fde4278b77468a240fed87997f7 - Sigstore transparency entry: 186068419
- Sigstore integration time:
-
Permalink:
timvink/sudoku-solver@924f6c61f84d95d9a6d05f46a1ea8af8f84288fa -
Branch / Tag:
refs/heads/main - Owner: https://github.com/timvink
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_to_pypi.yml@924f6c61f84d95d9a6d05f46a1ea8af8f84288fa -
Trigger Event:
workflow_dispatch
-
Statement type: