Skip to content

A moderately fast sudoku solver in python using Algorithm X and exact cover. I did this for a module at university. [Awarded 100%]

Notifications You must be signed in to change notification settings

TheGogy/sudoku-solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sudoku Solver using Exact Satisfactability and Algorithm X

For more information on how to use this solver, please refer to my blog post.

How to use the solver

Solver - sudoku.py

To use the solver, pass a numpy.ndarray((9x9)<sudoku>) to sudoku.py. The test script included (test_sudoku.py) contains examples of how to do this and examples of input are stored in numpy arrays in /data/.

import numpy as np
from sudoku import sudoku_solver

initial_state = np.ndarray((9,9), <your sudoku here>)
solution = sudoku_solver(initial_state)

...

The solver will return the solved sudoku in the form numpy.ndarray((9x9)<solution). If the sudoku has multiple possible solutions, the solver will return the first one it finds.

Test script - test_sudoku.py

I have included a test script test_sudoku.py, that implements the test package test_scripts.py (see below) with the sudoku solver.

Usage is as follows:

Usage:
   test_sudoku.py [ -q , -p , -o , -h|-n|-m|-f ] <batches | filename>

Tests sudoku solver with various puzzles.

Options:
   -h, --help              Show help.

   -n, --normal            Run test as normal.

   -m, --multiple          Run <batches> tests of normal test.

   -f, --file              Solve a sudoku stored in a file
                           [*.txt, *.npy]

   -o, --output-to-file    Output the solutions to a .npy file
                           (use with -f)

   -p, --use-process-time  Use time.process_time() instead of
                           time.perf_counter()

   -q, --quit-after        Stop solving sudokus after it has solved a certain
                           number. Used with "-f" on .npy and .csv files.

About

A moderately fast sudoku solver in python using Algorithm X and exact cover. I did this for a module at university. [Awarded 100%]

Topics

Resources

Stars

Watchers

Forks

Languages