Skip to main content

Solve ubongo puzzle by constraint programming

Project description

🧩 UbongoSolve

A Python solver for Ubongo-style tile-placement puzzles using constraint programming (OR-Tools CP-SAT).

Given a set of polyomino pieces and a board, UbongoSolver finds a placement that tiles the board exactly — no gaps, no overlaps.

Features

  • Automatic piece orientation: handles all rotations (0°/90°/180°/270°) and flips
  • Constraint programming engine: powered by Google OR-Tools CP-SAT for fast, exact solving
  • Visualization: text-based and matplotlib-based solution display with colored pieces and piece-boundary outlines
  • Built-in puzzles: includes the White Chocolate puzzle with a few board variants

Installation

Requires Python 3.10+.

pip install ubongosolve

From source

git clone https://github.com/kota7/ubongosolve.git
cd ubongosolve
pip install -U .

Quick Start

from ubongosolve import Piece, Board, UbongoPuzzle

# Define pieces as sets of (x, y) coordinates
pieces = [
    Piece([(0,0), (0,1), (0,2), (0,3), (1,3)]), # L-shape
    Piece([(0,0), (1,0), (1,1), (2,1)]),        # S-shape
    Piece([(0,0), (0,1), (1,1)]),               # mini L-shape
]

# Define a board
board = Board([(x, y) for x in range(4) for y in range(3)])

# Solve
solver = UbongoPuzzle(pieces, board)
status = solver.solve()
print(status)  # "OPTIMAL" or "FEASIBLE"

# View the solution
solver.print_solution()     # text output
solver.plot_solution()      # matplotlib figure

Example: Ubongo Sample problem

from ubongosolve import UbongoPuzzle, ubongo

problem = ubongo.sample_problems[0]  # Two problems are included
solver = UbongoPuzzle(problem["pieces"], problem["board"])
solver.solve()
solver.plot_solution()

Example: White Chocolate puzzle

from ubongosolve import UbongoPuzzle, whitechocolate

# Solve the basic 5×8 board
solver = UbongoPuzzle(whitechocolate.pieces, whitechocolate.board)
solver.solve()
solver.plot_solution()

# Try a challenge board
solver = UbongoPuzzle(whitechocolate.pieces, whitechocolate.boards["challenge_6"])
solver.solve()
solver.plot_solution()

API Reference

Piece(coordinates)

A polyomino piece defined by a set of (x, y) cell coordinates. Coordinates are automatically normalized so the minimum x and y are zero.

Board(coordinates)

A board defined by a set of (x, y) cell coordinates. Can be any shape — rectangles, L-shapes, boards with holes, split boards, etc.

UbongoPuzzle(pieces, board)

Method / Property Description
solve(timeout=100) Solve the puzzle. Returns status string ("OPTIMAL", "FEASIBLE", "INFEASIBLE", ...)
solution dict[tuple[int,int], int] mapping each cell to its piece ID
print_solution() Print an ASCII representation
plot_solution() Display a matplotlib figure with colored pieces
solution_as_fig Returns (fig, ax) without displaying

How It Works

  1. Piece expansion: each piece is expanded into all distinct orientations (up to 8: 4 rotations × 2 flips).
  2. Origin candidates: for each orientation, all valid placements on the board are precomputed.
  3. CP-SAT model: boolean variables represent "piece i in orientation o is placed at origin (x, y)". Constraints enforce:
    • Each piece is placed exactly once (one orientation + origin selected).
    • Each board cell is covered by exactly one piece.
  4. Solve: OR-Tools CP-SAT finds a feasible assignment or proves infeasibility.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ubongosolve-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ubongosolve-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file ubongosolve-0.1.0.tar.gz.

File metadata

  • Download URL: ubongosolve-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ubongosolve-0.1.0.tar.gz
Algorithm Hash digest
SHA256 797b62fc73960393e4652e21f8e3de357ca551acf8346e855c25e25c2dcbd89f
MD5 13fe084557e519b248663e74bf9cc2b7
BLAKE2b-256 4b3433c80ec54c22b64ef1cfd6f7ee52750a0deaa1430932682070cca2c16abe

See more details on using hashes here.

File details

Details for the file ubongosolve-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ubongosolve-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ubongosolve-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64c6ba979aa0250cee074a2bb85ae66e1669154afb38ae83c0ca026059ba4a64
MD5 ea8895defb9ff14682291528aac61f43
BLAKE2b-256 894d975c9a85733c003ba01b5e5c5cec7a9c40c5d049cdaaf83b887fd57f007f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page