Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maxme1/sudoku
A fast sudoku solver
https://github.com/maxme1/sudoku
Last synced: about 2 months ago
JSON representation
A fast sudoku solver
- Host: GitHub
- URL: https://github.com/maxme1/sudoku
- Owner: maxme1
- Created: 2019-12-11T07:51:22.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-05T19:05:01.000Z (about 4 years ago)
- Last Synced: 2024-10-29T23:16:14.984Z (3 months ago)
- Language: Python
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
This library contains a fast sudoku solver
that generates all possible solutions to a given puzzle of any shape.### Usage
```python
from sudoku import solve, print_field# input as text
puzzle = '''
1 ? 9 ? ? ? 6 ? ?
? ? ? ? ? ? ? ? ?
8 ? ? 4 9 7 ? ? 1
? ? 5 ? ? 2 ? ? ?
? ? 6 7 ? ? ? 1 ?
? ? 1 ? ? ? ? ? 7
7 ? ? 9 4 6 ? ? 2
? 1 ? ? ? ? ? ? ?
? ? 3 ? 7 1 9 ? 8
'''# the solution is a simple numpy array
solution = next(solve(puzzle))
print_field(solution)
# outputs
# -------------------------
# | 1 3 9 | 2 8 5 | 6 7 4 |
# | 5 4 7 | 1 6 3 | 2 8 9 |
# | 8 6 2 | 4 9 7 | 3 5 1 |
# -------------------------
# | 3 7 5 | 8 1 2 | 4 9 6 |
# | 2 9 6 | 7 5 4 | 8 1 3 |
# | 4 8 1 | 6 3 9 | 5 2 7 |
# -------------------------
# | 7 5 8 | 9 4 6 | 1 3 2 |
# | 9 1 4 | 3 2 8 | 7 6 5 |
# | 6 2 3 | 5 7 1 | 9 4 8 |
# -------------------------
```
The input can be a numpy array, check `solve`'s docstring for details.
There is also a handy shortcut for solving and printing the solutions:
```python
from sudoku import print_solutionsprint_solutions(puzzle, max_solutions=2)
# outputs
# -------------------------
# | 1 3 9 | 2 8 5 | 6 7 4 |
# | 5 4 7 | 1 6 3 | 2 8 9 |
# | 8 6 2 | 4 9 7 | 3 5 1 |
# -------------------------
# | 3 7 5 | 8 1 2 | 4 9 6 |
# | 2 9 6 | 7 5 4 | 8 1 3 |
# | 4 8 1 | 6 3 9 | 5 2 7 |
# -------------------------
# | 7 5 8 | 9 4 6 | 1 3 2 |
# | 9 1 4 | 3 2 8 | 7 6 5 |
# | 6 2 3 | 5 7 1 | 9 4 8 |
# -------------------------
#
# -------------------------
# | 1 3 9 | 2 8 5 | 6 7 4 |
# | 5 4 7 | 1 6 3 | 8 2 9 |
# | 8 6 2 | 4 9 7 | 3 5 1 |
# -------------------------
# | 3 7 5 | 8 1 2 | 4 9 6 |
# | 4 8 6 | 7 5 9 | 2 1 3 |
# | 2 9 1 | 6 3 4 | 5 8 7 |
# -------------------------
# | 7 5 8 | 9 4 6 | 1 3 2 |
# | 9 1 4 | 3 2 8 | 7 6 5 |
# | 6 2 3 | 5 7 1 | 9 4 8 |
# -------------------------
```