-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a similar Python program for comparison purposes, along with a …
…sample complex board.
- Loading branch information
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |