Skip to main content

grilops

a GRId LOgic Puzzle Solver library, using Python 3 and z3.

This package contains a collection of libraries and helper functions that are useful for solving and checking Nikoli-style logic puzzles using z3.

To get a feel for how to use this package to model and solve puzzles, try working through the tutorial IPython notebook, and refer to the examples and the API Documentation.

Installation

grilops requires Python 3.6 or later.

To install grilops for use in your own programs:

$ pip3 install grilops

To install the source code (to run the examples and/or work with the code):

$ git clone https://github.com/obijywk/grilops.git
$ cd grilops
$ pip3 install -e .

Basic Concepts and Usage

The symbols, geometry, and grids modules contain the core functionality needed for modeling most puzzles. For convenience, their attributes can be accessed directly from the top-level grilops module.

Symbols represent the marks that are determined and written into a grid by a solver while solving a puzzle. For example, the symbol set of a Sudoku puzzle would be the digits 1 through 9. The symbol set of a binary determination puzzle such as Nurikabe could contain two symbols, one representing a black cell and the other representing a white cell.

The geometry module defines Lattice classes that are used to manage the shapes of grids and relationships between cells. Rectangular and hexagonal grids are supported, as well as grids with empty spaces in them.

A symbol grid is used to keep track of the assignment of symbols to grid cells. Generally, setting up a program to solve a puzzle using grilops involves:

  • Constructing a symbol set
  • Constructing a lattice for the grid
  • Constructing a symbol grid in the shape of the lattice, limited to contain symbols from the symbol set
  • Adding puzzle-specific constraints to cells in the symbol grid
  • Checking for satisfying assignments of symbols to symbol grid cells

Grid cells are exposed as z3 constants, so built-in z3 operators can and should be used when adding puzzle-specific constraints. In addition, grilops provides several modules to help automate and abstract away the introduction of common kinds of constraints.

Paths

The grilops.paths module is helpful for adding constraints that ensure symbols connect to form paths through the grid. These paths may be either closed (loops) or open ("terminated" paths). Some examples of puzzle types for which this is useful are Numberlink and Slitherlink.

$ python3 examples/numberlink.py        $ python3 examples/slitherlink.py
┌─┐4──┐                                 ┌──┐
│3└─25│                                 │┌┐│ ┌┐
│└─31││                                 └┘│└┐││
│┌─5│││                                   │ └┘│
││┌─┘││                                   └┐  │
││1┌─┘│                                 ┌──┘┌┐│
2└─┘4─┘                                 └───┘└┘

Unique solution                         Unique solution

Regions

The grilops.regions module is helpful for adding constraints that ensure cells are grouped into orthogonally contiguous regions (polyominos) of variable shapes and sizes. Some examples of puzzle types for which this is useful are Nurikabe and Fillomino.

$ python3 examples/nurikabe.py          $ python3 examples/fillomino.py 
2 █   ██ 2                              8 8 3 3 101010105               
███  █2███                              8 8 8 3 1010105 5               
█2█ 7█ █ █                              3 3 8 10104 4 4 5               
█ ██████ █                              1 3 8 3 102 2 4 5               
██ █  3█3█                              2 2 8 3 3 1 3 2 2               
 █2████3██                              6 6 2 2 1 3 3 1 3               
2██4 █  █                               6 4 4 4 2 2 1 3 3               
██  █████                               6 4 2 2 4 3 3 4 4               
█1███ 2█4                               6 6 4 4 4 1 3 4 4               
                                                                        
Unique solution                         Unique solution

Shapes

The grilops.shapes module is helpful for adding constraints that ensure cells are grouped into orthogonally contiguous regions (polyominos) of fixed shapes and sizes. Some examples of puzzle types for which this is useful are Battleship and LITS.

$ python3 examples/battleship.py        $ python3 examples/lits.py
     ▴                                        IIII
◂▪▸  ▪ •                                   SS  L  
     ▾                                   LSS   L I
◂▪▪▸   •                                 L IIIILLI
                                         LL   L  I
 ▴    ◂▸                                  TTT L  I
 ▾ ▴                                    SS T LL  T
   ▾ •                                   SSLL   TT
                                            L T  T
Unique solution                         IIIILTTT

                                        Unique solution

Sightlines

The grilops.sightlines module is helpful for adding constraints that ensure properties hold along straight lines through the grid. These "sightlines" may terminate before reaching the edge of the grid if certain conditions are met (e.g. if a certain symbol, such as one representing a wall, is encountered). Some examples of puzzle types for which this is useful are Akari and Skyscraper.

$ python3 examples/akari.py             $ python3 examples/skyscraper.py 
█* █*    █                              23541                            
   *   █                                15432                            
*█*   █  *                              34215                            
 *█  █   █                              42153                            
   ███*                                 51324                            
   *███*                                                                 
█ * █* █*                               Unique solution
*  █*   █*
  █     * 
█ *   █* █

Unique solution

Release files for grilops 0.10.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grilops 0.10.3
File Size Uploaded
grilops-0.10.3.tar.gz 24.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for grilops 0.10.3
File Interpreter ABI Platform
grilops-0.10.3-py3-none-any.whl Python 3 none any Details

Total release size: 52.2 kB

Release files / grilops-0.10.3.tar.gz

Download URL grilops-0.10.3.tar.gz
Size 24.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ee0871b402cf7be43a40314e7e8f4280f930ca413e0cb9533188b024e240d2e7
BLAKE2b-256 checksum
How to use checksums
8edb71ff2edda0e519d7e921c711d792e5f1b7830aaebc19cd1686cf2692ce03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Release files / grilops-0.10.3-py3-none-any.whl

Download URL grilops-0.10.3-py3-none-any.whl
Size 27.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e665d592b34dd09cb6d31bb5cbfbb55369268a097983955c6a87995a3bd011a0
BLAKE2b-256 checksum
How to use checksums
ac0a5e4531a51166f75f61d3b1aff6f048652a077061d69a8c88f10c02699de4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Release history Release notifications | RSS feed

This release

0.10.3 This release

2 release files

0.10.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page