https://github.com/timvink/sudoku-solver
Sudoku solver built in Python
https://github.com/timvink/sudoku-solver
Last synced: about 1 year ago
JSON representation
Sudoku solver built in Python
- Host: GitHub
- URL: https://github.com/timvink/sudoku-solver
- Owner: timvink
- License: gpl-3.0
- Created: 2022-07-10T19:30:17.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-05-04T02:55:57.000Z (about 1 year ago)
- Last Synced: 2025-05-04T03:32:46.190Z (about 1 year ago)
- Language: Python
- Size: 260 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/timvink/sudoku-solver/actions)



# 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](https://timvink.nl/blog/introducing-sudoku-solver/).
## Setup
Install the package:
```bash
pip install sudoku-solver-tim
```
## Usage
```python
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:
```python
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:
- [Single Candidates](https://www.sudokuoftheday.com/techniques/single-candidate)
- [Single Position](https://www.sudokuoftheday.com/techniques/single-position)
Medium:
- [Candidate Lines](https://www.sudokuoftheday.com/techniques/candidate-lines)
- [Double Pairs](https://www.sudokuoftheday.com/techniques/double-pairs)
- [Multiple Lines](https://www.sudokuoftheday.com/techniques/multiple-lines)
Advanced:
- [Naked Pairs](https://www.sudokuoftheday.com/techniques/naked-pairs-triples)
- [Naked Triples](https://www.sudokuoftheday.com/techniques/naked-pairs-triples)
- [Naked Quads](https://www.sudokuoftheday.com/techniques/naked-pairs-triples)
- [Hidden Pairs](https://www.sudokuoftheday.com/techniques/hidden-pairs-triples)
- [Hidden Triples](https://www.sudokuoftheday.com/techniques/hidden-pairs-triples)
- [Hidden Quads](https://www.sudokuoftheday.com/techniques/hidden-pairs-triples)
Master:
- [X-Wings](https://www.sudokuoftheday.com/techniques/x-wings)
- [Swordfish](https://www.sudokuoftheday.com/techniques/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](https://www.sudokuoftheday.com/techniques/forcing-chains), [Nishio](https://www.sudokuoftheday.com/techniques/nishio) and [Guessing](https://www.sudokuoftheday.com/techniques/guesswork).
Some remarks:
- We have not implemented [Y-wings](https://sudoku.com/sudoku-rules/y-wing/), although you do not need them given the other strategies.
- The implementation of `swordfish` included both Swordfish-3 and Swordfish-4. Swordfish-4 is sometimes called "Jellyfish", and could be a separate strategy.
- [Forcing Chains](https://www.sudokuoftheday.com/techniques/forcing-chains) is not guesswork/brute force, but it's a lot of hard work if you had to do it by hand.