Computer scientists have a name for the kind of problem puzzle solvers tackle every day: constraint satisfaction. Thinking in those terms, even informally, makes many puzzles easier to reason about. You don’t need any programming to use the idea. And it isn’t just theory for us: the programs we use to check every Sudoku, nonogram and logic grid on this site are constraint solvers, and what they’ve shown us about our own puzzles is a good way to explain how it works.
The Three Parts
Every constraint problem has three parts:
- Variables: the things you’re trying to work out. In Sudoku, the empty squares. In a nonogram, each square (filled or empty). In a logic grid, who goes with what.
- Domains: the values each variable could take. The digits 1 to 9; filled or blank; the five laptops.
- Constraints: the rules. No repeated digit in a row, column or box. Each row must match its clue numbers. Each person gets exactly one item.
Naming the parts makes the job clear: shrink the domains using the constraints until every variable has one value left.
Propagation: The Engine
Propagation means using what you know to remove options elsewhere. Place a 7 in Sudoku, and every other square in its row, column and box loses 7 as a candidate. That’s exactly what you’re doing when you keep your pencil marks up to date.
Propagation is far more powerful than it sounds. When we run our 37 nonograms through a solver that does nothing but propagate (look at one line, work out every square that must be filled or empty, move to the next line, repeat), it solves 32 of them completely. No clever tricks, just thorough propagation.
The practical lesson: before you reach for an advanced technique, make sure you’ve propagated everything. A very common stall is a placed digit you never fully crossed out of its row, column and box.
Looking at a Whole Constraint at Once
Simple propagation looks at one square at a time: “what can go here?” Stronger reasoning looks at a whole constraint at once: “this row needs a 7 somewhere; where can it go?” Computer scientists call the stronger version enforcing consistency on the constraint. In Sudoku, that’s the difference between a naked single and a hidden single, and it matters enormously. On our Hard Sudoku #1, the one-square view (naked singles) can’t place a single digit at the start. The whole-row view (hidden singles) fills it to 44 squares.
When Propagation Runs Out: Search
If propagation can’t finish, a computer makes a choice: it tries a value, propagates again, and if it hits a contradiction, it backs up and tries the next value. That’s called backtracking, and it’s the same thing as the “what if” test human solvers use.
Our nonograms show how much of it each puzzle needs. The five that propagation can’t finish all need some testing: the Owl needs exactly one test, while the Smiley Face needs 18. That’s why the Owl is rated Medium and the Smiley Face is rated Hard.
One rule from computer science helps human solvers a lot: when you test, test the variable with the fewest options. In Sudoku, that’s a square with just two candidates. In a nonogram, it’s a square on a short, tightly packed line. The fewer the options, the faster one of them breaks, and the less you have to undo.
Checking a Puzzle Is Fair
The same tools tell us whether a puzzle is fair before we publish it:
- One solution only. We let the solver search for every solution, not just the first. If it finds two, the puzzle is broken. Our free Sudoku solver now does this for any grid you type in, and warns you if there’s more than one answer.
- Every clue needed. For each logic grid, we remove one clue at a time and count solutions again. If the puzzle still has only one answer without a clue, that clue was dead weight. On every grid we publish, removing any single clue leaves two or more answers.
The Takeaway
Name the variables, domains and constraints; propagate everything before you try anything clever; look at whole rows and boxes, not just single squares; and when you must test, test the square with the fewest options. That’s how a computer solves puzzles, and it’s a very good way for a person to solve them too.