How the solver works
Pips is the New York Times’ daily domino logic puzzle: lay a fixed set of dominoes on a board so that every shaded region’s rule comes out true. Underneath, it’s a constraint-satisfaction problem, NP-hard in general. Here’s how this solver cracks it in (almost always) under a millisecond.
Watch it backtrack
A tiny puzzle: place a piece, hit a dead end, recover.
An empty board. The teal region (the top-left three cells) must add up to exactly 8.
Pips as a constraint problem
A puzzle is three things: a set of cells (the board can be any shape, with holes), a collection of dominoes, each a pair of pips from 0 to 6, and a set of regions, each with a rule. A solution covers every cell with dominoes so that every region rule holds at once.
The rules all come down to simple totals over the cells a region covers:
- sum = N
- the covered pips add up to exactly N
- equal
- every covered pip is the same number
- unequal
- every covered pip is different
- < N, > N
- the covered pips add up to less than, or more than, N
- empty
- no rule; any pips are fine
Unlike classic dominoes, touching halves don’t need to match. Only the region rules matter, which is what makes the search interesting.
Backtracking, most-constrained cell first
At heart it’s depth-first backtracking: place a domino, recurse, and undo it on failure. The leverage comes from which cell to fill next. Instead of reading order, the solver uses the minimum-remaining-values (MRV) heuristic: it always fills the empty cell with the fewest empty neighbours, the most boxed-in one, and stops looking once it finds a cell with one option or none. Forced moves happen at once and dead ends surface early, which collapses the branching.
solve():
if every cell is filled -> SOLUTION
cell = the empty cell with the
fewest empty neighbours # MRV
for each empty neighbour n of cell:
for each remaining domino (a, b):
for each orientation:
place (a, b) over (cell, n)
if regionsValid() # incremental check
and noCellIsolated() # structural prune
and regionsStillSatisfiable(): # forward check
if solve(): return SOLUTION
undo the placement # backtrack
return FAILIncremental constraint checks
Rescanning the whole board after every placement would be wasteful, so the solver keeps live totals for each region: a running sum, a count of filled cells, and a tally of each value. From those, every rule is a constant-time test:
- sum fails fast once the partial sum passes the target;
- equal is just “at most one distinct value so far”;
- unequal is “as many distinct values as filled cells”.
A domino only ever touches the (at most two) regions under its two cells, so placing or removing one is a constant-time update, with no board sweep.
Forward checking with a pip pool
Before recursing, the solver asks a cheap but powerful question: can each unfinished region still be satisfied with the pips that are left? It keeps a pip pool, how many of each value 0–6 remain in unplaced dominoes, and from the smallest and largest sums still reachable it prunes branches where a target is out of reach, an “equal” region has run short of matching pips, or an “unequal” region has run out of distinct values. Each test is a necessary condition, so it never throws away a real solution; it just cuts doomed branches early.
Cheap structural cuts
- No isolated cells. A placement that strands an empty cell with no empty neighbour can never be finished by a 1×2 domino, so it’s rejected. Only cells next to the new piece can become isolated, so the check stays local.
- Symmetric dominoes. A piece like 3|3 looks the same flipped, so its second orientation is skipped, halving that piece’s branching.
Flat, allocation-free state
Everything constant is worked out once into typed arrays: neighbour lists, each cell’s regions, and the domino pip pairs. The board is an Int8Array, the pip pool an Int32Array, and the per-region tallies are typed arrays too. The hot loop has no string keys, no Map rebuilds and no per-step allocations, which keeps it fast under heavy backtracking.
What the numbers look like
The worst case is exponential, but real NYT puzzles are far from it once MRV and propagation do their jobs:
| Difficulty | Placements tried | Time |
|---|---|---|
| Easy | ~10–60 | under 1 ms |
| Medium | hundreds to low thousands | about 1 ms |
| Hard | up to ~10⁵ | tens of ms |
A few puzzles have more than one valid answer; the solver returns the first it proves correct.