Skip to main content

Sudoku solver that will use successively more advanced techniques to solve the puzzle.

Project description

Actions Status PyPI - Python Version PyPI PyPI - License

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 swordfish included 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sudoku_solver_tim-0.1.1.tar.gz (154.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sudoku_solver_tim-0.1.1-py3-none-any.whl (37.1 kB view details)

Uploaded Python 3

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

Hashes for sudoku_solver_tim-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6959ea72604dee666de50c121449096c4c221cd1d7b696ed8f39de8e3ef3361d
MD5 a53c94aa3bbbc38a61d5b8e5551a210d
BLAKE2b-256 aece6f483758e3c44f8dad10dc910175736c79ea6d187736abddee716cd37698

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sudoku_solver_tim-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sudoku_solver_tim-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aaa9e8fd296630286fe14dc75feca7b5945e2fde4278b77468a240fed87997f7
MD5 ad44822f7497fa8e634123a6d0278664
BLAKE2b-256 ecb88ca53ec290e55fda91858b18571b7fec15f08c64460a5530e3288f8417dd

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page