Skip to content

solvingmethods

Core module of the solver, providing the structure of the different solving algorithms by means of the base classes FmtSolvingBase and FmtSolvingMethod. These classes work as templates for the integration of the specific solving algorithms through their child classes but also provide the abstract interface to access these algorithms.

Use this module to integrate your own solving algorithms to this solver or to access the four default solving methods: NTilesNOptions, ScaledXWing, YWing and Bifurcation.

Moreover, this file also provides the device required to remove candidates from Sudoku tiles. Observe that the implemented algorithms are not able to solve the puzzle on their own but depend on a prescription of what algorithm to use in the specific state of the puzzle. This logic is implemented by means of any solving-method class having attributes that link to other solving-method classes which are to be used depending on the success or failure of the present solving algorithm. The linking of the solving methods is done via the generate_solver function found in solver.py

Bifurcation #

Bases: FmtSolvingMethod

The uninspired emergency solving method.

After failure of the previous algorithms, we may still fall back to a trial and error approach by considering tiles with two candidates left and by mindlessly picking one of them as the tiles value. From there on, we can continue solving the puzzle with the 'more analytic' methods until we either solve to puzzle or run into a violation of the Sudoku rules. Since we only apply this approach to 'two-candidate' tiles, a failure of the try still immediately fixes the definitive value of the considered tile.

FmtParamSolvingMethod #

Bases: FmtSolvingMethod

Base class for solving methods that depend on a parameter. The minimum value the parameter takes is specified by the _N_MIN class variable which varies between the different solving method classes that inherit from the present class.

The solving method's parameter is set at initialization of any such object; if the given parameter is smaller than _N_MIN, a corresponding error terminates the program.

FmtSolvingBase #

Class to define the basic structure of any object the Sudoku solver consists of. Such child classes must always implement a launch method which, in the case of the RemoveAndUpdate class, invokes the removal process whereas in the case of any regular solving method (i.e. of any class inheriting from FmtSolvingMethod), the respective solving algorithm will be invoked.

Any such class has a _stepper attribute which is the tool needed to guide the user through the solving process and to collect information thereof.

launch(S, *args) abstractmethod #

Interface method to invoke the internals of the corresponding solving algorithm.

FmtSolvingMethod #

Bases: FmtSolvingBase

Base class to define the structure of the solver.

Any class inheriting from this base represents an algorithm used to eliminate candidate options from the unsolved puzzle.

The implementation of these algorithms happens by means of overloading the abstract launch method. This method, taking the unsolved puzzle as argument, executes the corresponding solving algorithm and depending on its success decides what solving method to try next. That is, if the present algorithm succeeds at eliminating at least one candidate option, then the launch method of the _advance attribute is invoked. On the other hand, if the present algorithm fails at removing candidates, the unsolved puzzle is passed to the launch implementation of the _fall_back attribute. Both, the _advance and the _fall_back object must therefore be an instances of a children of FmtSolvingMethod themselves.

If none of the implemented solving methods (i.e. non of the instances of the corresponding children of FmtSolvingMethod) manage to contribute to the solution, the puzzle must be considered unsolvable. Therefore, the _fall_back attribute defaults to _SolvingFail() which is a trivial child of FmtSolvingMethod whose launch method raises an error. Hence, if any solving-method object does not explicitly link a _fall_back instance, it is automatically considered to represent the worst case algorithm to be tried as its failure implies the insolubility of the puzzle by this solver.

launch(S) abstractmethod #

Interface method to invoke the internals of the Solving Algorithms.

Parameters:

Name Type Description Default
S Sudoku

The Sudoku puzzle to which the algorithm is applied

required

LoneSingles #

Bases: FmtSolvingMethod

The most basic solving method that is used to initiate the solving process. This method simply searches for the tiles whose value has already been fixed and removes the respective number from its neighboring tiles.

NTilesNOptions #

Bases: FmtParamSolvingMethod

A solving method that checks if a set of the same n options is found n times in the same row, column or square. If, e.g., the set of candidate options {1,2} is found twice in the same row, i.e., if two separate tiles have the exact same two candidates left, no other tile in the same row may take any of the latter two values. Notice that for n=1, this method is equivalent to LoneSingles.

RemoveAndUpdate #

Bases: FmtSolvingBase

Special solving-method class whose launch method implements the functionality to remove candidates from specified tiles. Correspondingly, this method should be invoked by the 'regular' solving-method objects, i.e., by instances of classes that inherit from FmtSolvingMethod.

Moreover, the removal process also triggers the clean up methods _single_occurrence_of_option and _remove_option_from_neighbors which implement further candidate removal steps that are directly implied by the initial removal.

launch(S, where, which) #

Interface to initiate the removal of the candidates which form the tile at where of the Sudoku S.

Parameters:

Name Type Description Default
S Sudoku

The puzzle

required
where int

Index of the concerned tile

required
which set

Candidates to be removed

required

Returns:

Type Description
bool

Could any candidates be removed?

RemoverMissingError #

Bases: NotImplementedError

No remover has been assigned to the solving method.

ScaledXWing #

Bases: FmtParamSolvingMethod

Solving methods that searches for candidate options appearing in an n x n square.

Consider, e.g., two columns in which the same candidate occurs at exactly two positions. Now, let the rows in which these occurrences are to be the same for both columns. Then non of the remaining tiles within the latter tow rows can take this candidate as value anymore as its position is already fixed to a place in either of the tow columns we considered initially.

SolverError #

Bases: RuntimeError

The puzzle can't be solved with the given solver.

StepperMissingError #

Bases: NotImplementedError

No stepper has been assigned to the solving method.

YWing #

Bases: FmtSolvingMethod

Implementation of the classical 'Y-Wing' strategy.

This methods first searches for an 'anchor' tile, i.e., a tile with two candidates left, say {1,2}. From this tile on, the neighboring row, column and square are searched for further 'two-candidate' tiles. The aim, thereby, is to find two more tiles, one of them including 1 and the other 2 as candidate. Most importantly these two tiles must also have one common candidate, say 3, such that we end up with two 'node' tiles related to the anchor, having candidates {1,3} and {2,3}. No matter what configuration eventually solves the puzzle, one of the node tiles must be assigned the value 3 as fixing the value of one node immediately fixes the value of the other node via the anchor tile. Consequently, 3 can be excluded as candidate from any tile that is simultaneously related to both nodes.