Constraint Solvers and LLMs: Solving Combinatorial Puzzles Beyond Model Capabilities
This article examines a 9×9 combinatorial puzzle involving circular arcs, integer-area regions, region scores, and uniqueness checks. It explains why pure LLM reasoning and local brute-force enumeration run into combinatorial explosion and memory limits, then describes a division of labor in which the LLM helps formalize rules and write tests while a constraint solver handles search, pruning, and feasibility checking. It also covers the modeling steps, pseudocode structure, independent verification, and engineering practices for separating heuristic guesses from verified results.
1. Problem Background
Some organizations periodically publish combinatorial math puzzles. The rules are usually concise, but the task requires finding the unique consistent solution among a large number of possibilities. Consider a 9×9 grid puzzle. The rules include the following: place 90-degree circular arcs of radius 1 in white cells; the arcs divide the grid into regions; each region must have an integer area; the score of a region is defined as its area multiplied by the number of smooth boundary segments; the score of the region containing a numbered cell must equal that number; and the final answer is computed from the sum of squares of row sums and column sums.
Such puzzles look simple locally but are highly coupled globally. The position of one arc affects the area, boundary shape, and score of adjacent regions at the same time. Any local decision can create a contradiction far away, so solving the puzzle requires maintaining all constraints simultaneously.
2. Practice and Intermediate Results
The author followed a typical programmatic route. The first step was to write a geometry engine that could compute smooth segment counts and region areas for any arc configuration. The second step was to enumerate all geometrically valid local solutions for each numbered cell. The third step was to use constraint propagation to gradually reduce the candidate space.
This route worked at the local stage. For the central region alone, there were already thousands of local candidates. When the search advanced to a stage with more regions, the set used to record visited states expanded rapidly, memory usage exceeded 12GB, and the operating system’s memory protection mechanism terminated the process twice.
This result shows that the bottleneck was not single-point computation, but global state storage and combination.
3. Why Pure Reasoning Struggles to Converge
Pure reasoning faces four structural obstacles in this type of problem.
- Combinatorial explosion: The number of states grows exponentially with the number of regions. Checking the joint consistency of a dozen or so regions is essentially a feasibility test in a product space.
- Cost of state representation: If every local configuration is expanded into an explicit state, memory usage grows with the number of states and quickly exceeds what a single machine can handle.
- Long-chain consistency: The reasoning process must maintain the same set of constraints across dozens of steps. If any step misses a rule, subsequent conclusions become invalid.
- Lack of automatic verification: A one-shot numerical answer often comes without a constraint-by-constraint check, so readers cannot judge whether its source is reliable.
These obstacles have little to do with the language ability of a model. They mainly come from a mismatch between problem scale and solving method.
4. Reflecting on the Results
When a complete solution could not be produced, the author had the model perform greedy filling under different assumption scenarios and produce three candidate values. Such results are essentially heuristic estimates. Their credibility depends on whether the assumptions hold, not on whether they have been verified. The author also considers these candidate values almost certainly incorrect.
This can be summarized as one principle: distinguish “verified conclusions” from “evidence-based guesses.” Submitting a guess as a conclusion usually costs more than admitting that the task is not yet complete.
5. Capability Boundaries: What to Give to the Model and What to Give to the Solver
Different stages are suited to different tools. The following table shows a common division of labor.
| Task type | Suitable tool | Main reason |
|---|---|---|
| Understanding the problem, decomposing rules, proposing modeling ideas | Large language model | Strong semantic understanding and summarization |
| Writing modeling code and generating test cases | Large language model with a code execution environment | Results can be checked quickly |
| Large-scale search and feasibility checking | Constraint solver | Mature pruning and conflict learning mechanisms |
| Result verification | Independent verification program | Checks constraints one by one and produces reproducible results |
The value of the model lies in translating natural-language rules into formal descriptions. Exhaustive search and proof should be left to programs designed for those purposes.
6. Remodeling with a Constraint Solver
The core idea is to turn “search” into “declaring constraints,” leaving enumeration and pruning to the solver. Modeling usually has four steps.
- Define decision variables: the arc state of each white cell, whether each edge is crossed by an arc, and the region ID of each cell.
- Write geometry constraints: the arc choice for each cell can only take geometrically legal values, and green cells must not contain arcs.
- Write region constraints: region areas must be integers, the region score must equal its labeled number, and adjacent cells must have consistent region assignments.
- Define the objective: if the puzzle requires a unique solution, continue searching after finding one solution; if no other solution exists, uniqueness is established.
The following pseudocode illustrates the organization of the model:
# Pseudocode only to illustrate the modeling structurevariables:arc[r][c] ∈ {no_arc, orientation_A, orientation_B} for each white cellregion[r][c] ∈ integer region IDconstraints:for each cell: arc[r][c] must be a geometrically legal valuefor each region k: area(k) is an integerscore(k) = area(k) × smooth_segments(k)for each labeled cell (r, c, n):score(region[r][c]) == nfor each green cell: arc[r][c] == no_arcobjective:find a solution satisfying all constraints and verify its uniqueness
When modeling, first test the geometry engine with known valid small configurations, confirm that the semantics of each constraint are correct, and then scale up to the full grid.
7. Engineering Recommendations
- Position the model as a modeling assistant: let the model translate rules into formal constraints and write corresponding test cases.
- Write unit tests one by one: use manually constructed valid and invalid configurations to validate the geometry engine, preventing incorrect constraints from entering the search stage.
- Control memory growth in search: use incremental solving, decompose the problem into subproblems, exploit symmetry pruning, and set upper bounds and timeouts for state sets.
- Independently verify output results: only solutions that pass all constraint checks should proceed to later stages.
- Label the certainty of conclusions: annotate assumptions and confidence for heuristic results, and avoid packaging estimates as definitive answers.
When the search space of a problem exceeds what manual reasoning and general-purpose models can handle, the right approach is to hand it to tools designed for that purpose while retaining a human review step.
