Skip to content

pkkid/python-algorithmx

Repository files navigation

AlgorithmX Problems and Solvers

Trying to efficiently write a program to provide me solutions to Dragonfjord's "A Puzzle a Day" has lead me down a slightly mind boggling path of learning what about Donald Knuth's AlgorithmX, as well as Exact Cover, and Dancing Link algorithms. I am definitely not an expert in this algorithm, but was able to get something working with the help of Ali Assaf's post and implementation of AlgorithmX.

The examples in this repository can solve Sudoku and Pentomino puzzles relativly quickly. While all these examples work in Python3, I highly recomend using pypy as it is much faster for these types of problems.

Example Pentomino Solver

from pentomino import Puzzle

BOARD = """
β¬›β¬›πŸŸ«πŸŸ«πŸŸ«β¬›β¬›
🟫🟫🟫🟫🟫🟫🟫
🟫🟫🟫🟫🟫🟫🟫
πŸŸ«πŸŸ«πŸŸ«β¬›β¬›β¬›β¬›
"""
PIECES = """
πŸŸͺπŸŸͺπŸŸͺ β¬›πŸŸ¦β¬› ⬛πŸŸ₯πŸŸ₯ πŸŸ¨πŸŸ¨β¬› 🟩🟩🟩
⬛πŸŸͺ⬛ 🟦🟦🟦 πŸŸ₯πŸŸ₯⬛ πŸŸ¨πŸŸ¨β¬› β¬›β¬›πŸŸ©
"""

solver = Puzzle(BOARD, PIECES, allow_reflections=True)
solutions = list(solver.find_solutions())
for board in solutions:
    print(board)

>>>
⬛⬛πŸŸͺπŸŸ©πŸŸ©β¬›β¬›
🟦πŸŸͺπŸŸͺπŸŸͺ🟩🟨🟨
🟦🟦πŸŸ₯πŸŸ₯🟩🟨🟨
🟦πŸŸ₯πŸŸ₯⬛⬛⬛⬛

Example Dragonfjord Command Line Solver

Only a single random solution is displayed when running from the command line as seeing all solutions was a bit unwieldy.

> python3 puzzle-a-day.py --date=2025-05-29
πŸŸ¦πŸŸ¦πŸŸ¦πŸŸ¦β¬›πŸŸ§β¬›
πŸŸ¨πŸŸ¦πŸŸ¨πŸŸ§πŸŸ§πŸŸ§β¬›
🟨🟨🟨🟧⬜⬜⬜
🟩🟩🟩⬜⬜🟫🟫
🟩πŸŸͺπŸŸͺπŸŸ₯🟫🟫🟫
🟩πŸŸͺπŸŸͺπŸŸ₯πŸŸ₯πŸŸ₯πŸŸ₯
⬛πŸŸͺπŸŸͺ⬛⬛⬛⬛

Found 66 solutions after 5.5s.

To view all available options, run python3 puzzle-a-day.py --help. Available options for --puzzle are dragonfjord and guanglu, see puzzle-a-day.ini for the puzzle layouts.

Example Sudoku Solver

This solver is not my code, but Ali Assaf's. Copied from his posted example, cleaned up Python 3 styles, and commented to help me understand. I included it in this repo to hopefully help other's learn this algorithm usage as well.

from sudoku import solve_sudoku

grid = [
    [0,1,0, 0,0,0, 0,3,0],
    [9,0,0, 0,2,0, 1,5,0],
    [0,0,0, 1,0,0, 0,6,4],
    [7,0,0, 0,0,0, 0,0,0],
    [8,0,0, 3,9,0, 5,0,6],
    [0,0,0, 0,0,0, 0,4,9],
    [5,0,0, 0,7,1, 0,0,0],
    [0,0,8, 0,0,0, 0,9,1],
    [0,4,0, 2,6,0, 0,0,5]]
for solution in solve_sudoku(grid):
    print(*solution, sep='\n')
    print()

>>>
[4, 1, 2, 7, 5, 6, 9, 3, 8]
[9, 8, 6, 4, 2, 3, 1, 5, 7]
[3, 5, 7, 1, 8, 9, 2, 6, 4]
[7, 9, 1, 6, 4, 5, 8, 2, 3]
[8, 2, 4, 3, 9, 7, 5, 1, 6]
[6, 3, 5, 8, 1, 2, 7, 4, 9]
[5, 6, 3, 9, 7, 1, 4, 8, 2]
[2, 7, 8, 5, 3, 4, 6, 9, 1]
[1, 4, 9, 2, 6, 8, 3, 7, 5]

Thanks To

License

GNU General Public License http://www.gnu.org/licenses/

About

AlgorithmX Problems and Solvers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages