diff --git a/.gitignore b/.gitignore index f1c181e..55d76bb 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,105 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ \ No newline at end of file diff --git a/README.md b/README.md index 4034691..092c8a2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # rullo A solver for the Rullo game. +I've included Go and Python versions so you can compare ease, performance, resource consumption, etc. \ No newline at end of file diff --git a/board.py b/board.py new file mode 100644 index 0000000..0eeb4bf --- /dev/null +++ b/board.py @@ -0,0 +1,7 @@ +# sample board +board = [ + [1, 2, 3, 4, 6], + [5, 6, 7, 8,13], + [9, 10, 11, 12, 31], + [15, 12, 3, 20] +] \ No newline at end of file diff --git a/complex.py b/complex.py new file mode 100644 index 0000000..c4a9cbd --- /dev/null +++ b/complex.py @@ -0,0 +1,11 @@ +board = [ + [2, 2, 3, 4, 3, 4, 4, 3, 17], + [4, 2, 3, 4, 3, 3, 4, 4, 16], + [2, 2, 3, 2, 4, 4, 4, 4, 12], + [2, 4, 3, 3, 4, 4, 2, 4, 14], + [2, 4, 4, 2, 2, 4, 4, 2, 20], + [2, 4, 2, 4, 3, 3, 2, 2, 20], + [2, 2, 2, 2, 2, 4, 3, 2, 17], + [4, 3, 2, 3, 4, 4, 4, 2, 22], + [16, 19, 19, 18, 18, 22, 7, 19] +] diff --git a/complex.txt b/complex.txt new file mode 100644 index 0000000..4f7654d --- /dev/null +++ b/complex.txt @@ -0,0 +1,11 @@ +# 115 solutions +8 8 +2 2 3 4 3 4 4 3 17 +4 2 3 4 3 3 4 4 16 +2 2 3 2 4 4 4 4 12 +2 4 3 3 4 4 2 4 14 +2 4 4 2 2 4 4 2 20 +2 4 2 4 3 3 2 2 20 +2 2 2 2 2 4 3 2 17 +4 3 2 3 4 4 4 2 22 +16 19 19 18 18 22 7 19 \ No newline at end of file diff --git a/rullo.py b/rullo.py new file mode 100755 index 0000000..f8ef954 --- /dev/null +++ b/rullo.py @@ -0,0 +1,56 @@ +#! /usr/bin/python3 + +# rullo.py + +# A tool to solve a Rullo board. +import importlib, sys + +def vsum(board, col): + sum = 0 + for row in range(len(board)): + sum += board[row][col] + return sum + +def explore(board, solution, r, n, horz, vert): + global solutions + if r >= len(board): # we may have a solution + solved = True + for i in range(len(solution)): + if vsum(solution, i) != vert[i]: + solved = False + break + if solved: + solutions = solutions + 1 + print("{0:3d}: {1}".format(solutions, solution)) + else: + for i in range(1 << n): # exercise all elements + k = i + row = [] + for j in range(n): + row.append(0 if k % 2 == 0 else board[r][j]) + k >>= 1 + # did we solve this row? + if (sum(row)) == horz[r]: + solution[r] = row + # try the next row + explore(board, solution, r + 1, n, horz, vert) + +def solve(board): + global solutions + # extract sums from board + vert = board.pop() # vertical sums + horz = [] # horizontal sums + for r in range(len(board)): + horz.append(board[r].pop()) + print("board = {}\n horz = {}\n vert = {}".format(board, horz, vert)) + solutions = 0 + explore(board, board[:], 0, len(board[0]), horz, vert) + +if __name__ == "__main__": + # solve(board) + if len(sys.argv) < 2: + print("Usage: " + sys.argv[0] + " [...]") + else: + for b in range(1, len(sys.argv)): + m = importlib.import_module(sys.argv[b]) + solve(m.board) \ No newline at end of file