Skip to content

Commit

Permalink
Added a similar Python program for comparison purposes, along with a …
Browse files Browse the repository at this point in the history
…sample complex board.
  • Loading branch information
taflaj committed Apr 13, 2019
1 parent 23fb6c8 commit 36ce646
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
102 changes: 102 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions board.py
Original file line number Diff line number Diff line change
@@ -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]
]
11 changes: 11 additions & 0 deletions complex.py
Original file line number Diff line number Diff line change
@@ -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]
]
11 changes: 11 additions & 0 deletions complex.txt
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions rullo.py
Original file line number Diff line number Diff line change
@@ -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] + " <board file> [...]")
else:
for b in range(1, len(sys.argv)):
m = importlib.import_module(sys.argv[b])
solve(m.board)

0 comments on commit 36ce646

Please sign in to comment.