Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pkkid/python-algorithmx
AlgorithmX Problems and Solvers
https://github.com/pkkid/python-algorithmx
Last synced: 2 months ago
JSON representation
AlgorithmX Problems and Solvers
- Host: GitHub
- URL: https://github.com/pkkid/python-algorithmx
- Owner: pkkid
- Created: 2022-03-09T00:49:22.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T13:03:46.000Z (9 months ago)
- Last Synced: 2024-04-29T14:28:09.344Z (9 months ago)
- Language: Python
- Size: 32.2 KB
- Stars: 8
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AlgorithmX Problems and Solvers
Trying to efficiently write a program to provide me solutions to [Dragonfjord's
"A Puzzle a Day"](https://www.dragonfjord.com/product/a-puzzle-a-day/) has lead
me down a slightly mind boggling path of learning what about Donald Knuth's
[AlgorithmX](https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X), as well as
[Exact Cover](https://en.wikipedia.org/wiki/Exact_cover), and
[Dancing Link](https://en.wikipedia.org/wiki/Dancing_Links)
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](https://www.pypy.org/download.html) as it is much faster for these types
of problems.## Example Pentomino Solver
```python
from pentomino import PuzzleBOARD = """
โฌโฌ๐ซ๐ซ๐ซโฌโฌ
๐ซ๐ซ๐ซ๐ซ๐ซ๐ซ๐ซ
๐ซ๐ซ๐ซ๐ซ๐ซ๐ซ๐ซ
๐ซ๐ซ๐ซโฌโฌโฌโฌ
"""
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.
```bash
> 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.```python
from sudoku import solve_sudokugrid = [
[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
* [Donald Knuth](https://en.wikipedia.org/wiki/Donald_Knuth) for coming up with
[AlgorithmX](https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X).
* [Ali Assaf](https://www.cs.mcgill.ca/~aassaf9/index.html) for the short implementation
of AlgorithX as well as a [great write up and example](https://www.cs.mcgill.ca/~aassaf9/python/algorithm_x.html).## License
GNU General Public License