Skip to content

Latest commit

 

History

History
95 lines (72 loc) · 2.5 KB

README.md

File metadata and controls

95 lines (72 loc) · 2.5 KB

Sudoku solver

Python module which solves a sudoku puzzle

Code style: black

Usage

The module reads sudoku strings in sdm format.

Program

Invoke the program as follows to solve one puzzle:

python -m  sudokusolver --sdm 450109780027400013080627040805301200002095400314070000000000325030702194040503076

This prints the following:

450109780027400013080627040805301200002095400314070000000000325030702194040503076
456|139|782
927|458|613
183|627|549
------------
895|341|267
672|895|431
314|276|958
------------
761|984|325
538|762|194
249|513|876

It's also possible to specify a file containing one sdm entry per line:

python -m sudokusolver --file /path/to/sudoku.sdm

Use --help to display full options:

% python -m sudokusolver --help
usage: __main__.py [-h] (--sdm SDM | --file FILE) [--mode [{parallel,sequential}]] [--algorithm [{recursive,iterative}]]

options:
  -h, --help            show this help message and exit
  --sdm SDM             Sudoku puzzle in sdm format
  --file FILE           File containing sudoku puzzles in sdm format
  --mode [{parallel,sequential}]
                        How to process multiple boards. Default parallel
  --algorithm [{recursive,iterative}]
                        Default recursive

Package

from sudokusolver.board import Board
from sudokusolver.solver import solve

board = Board("450109780027400013080627040805301200002095400314070000000000325030702194040503076")
solution = solve(board)
print(solution.to_ss())

This prints the following:

456|139|782
927|458|613
183|627|549
------------
895|341|267
672|895|431
314|276|958
------------
761|984|325
538|762|194
249|513|876

Disclaimer

Many sudoku solver packages are already available on PyPI. This project doesn't pretend to compete with them 😉 In fact, PyPI has some solvers which are more advanced than this one, supporting boards of arbitrary size for example.

This project was simply a personal challenge. I made it open source in case any part of it could help anybody in some capacity.