Giter VIP home page Giter VIP logo

python-algorithmx's Introduction

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 relatively quickly. While all these examples work in Python3, I highly recommend 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 show a single random solution is displayed when running from the command line as seeing all solutions was a bit unwieldy.

> python3 dragonfjord.py --month=5 --day=29
๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆโฌ›๐ŸŸงโฌ›
๐ŸŸจ๐ŸŸฆ๐ŸŸจ๐ŸŸง๐ŸŸง๐ŸŸงโฌ›
๐ŸŸจ๐ŸŸจ๐ŸŸจ๐ŸŸงโฌœโฌœโฌœ
๐ŸŸฉ๐ŸŸฉ๐ŸŸฉโฌœโฌœ๐ŸŸซ๐ŸŸซ
๐ŸŸฉ๐ŸŸช๐ŸŸช๐ŸŸฅ๐ŸŸซ๐ŸŸซ๐ŸŸซ
๐ŸŸฉ๐ŸŸช๐ŸŸช๐ŸŸฅ๐ŸŸฅ๐ŸŸฅ๐ŸŸฅ
โฌ›๐ŸŸช๐ŸŸชโฌ›โฌ›โฌ›โฌ›

Found 66 solutions after 5.5s.

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/

python-algorithmx's People

Contributors

pkkid avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.