DEV Community

walle-liao
walle-liao

Posted on

Generating Unique-Solution Kakuro Puzzles and Grading Them by Solving Technique

Most online Kakuro (Cross Sums) generators either serve a tiny static pack or generate grids without proving uniqueness—which leads to frustrating late-game guessing.

While building Kakuro Club, I wanted every generated grid to satisfy three strict invariants:

  1. Mathematically unique solution (verified before a puzzle is ever served).
  2. Difficulty graded by human solving technique, not just grid size.
  3. Deterministic puzzle codes so the same short code always rebuilds the exact same grid for daily challenges and shared links.

Here is how the constraint solver and grading pipeline work under the hood.


1. Precomputing All 502 Valid Run Combinations

Every horizontal or vertical run in Kakuro uses k distinct digits from 1–9 summing to S. Across all run lengths k ∈ [2, 9], there are exactly 502 valid digit combinations, and 34 of them are one-way (unique) totals—meaning only one digit set can produce that sum.

Using bitmasks (1 << digit), precomputing the lookup table takes less than a millisecond:

import itertools

# Map (run_length, target_sum) -> list of 9-bit digit masks
COMBOS: dict[tuple[int, int], list[int]] = {}
for k in range(2, 10):
    for digits in itertools.combinations(range(1, 10), k):
        mask = 0
        for d in digits:
            mask |= 1 << (d - 1)
        COMBOS.setdefault((k, sum(digits)), []).append(mask)
Enter fullscreen mode Exit fullscreen mode

When the solver checks a run with partially filled cells, it filters COMBOS[(k, S)] against the bitmask of already-placed digits and intersects the union of remaining valid masks with each empty cell's candidate set.

I also published the full machine-readable dataset (JSON, CSV, and a zero-dependency lookup CLI) under the MIT license on GitHub: kakuro-combinations-dataset. For paper solvers, there is a one-page printable version on the Kakuro Cheat Sheet.


2. Proving Uniqueness Fast

A generated layout with filled numbers is only half the job—the clues could still admit two or more solutions. Instead of finding all solutions, the uniqueness verifier runs backtracking with Minimum Remaining Values (MRV) and aborts immediately once solutions == 2:

function countSolutions(grid: Grid, limit = 2): number {
  if (!propagateRunConstraints(grid)) return 0;
  const cell = pickCellWithFewestCandidates(grid);
  if (!cell) return 1; // solved

  let found = 0;
  for (const digit of candidatesOf(cell)) {
    const next = placeDigit(grid, cell, digit);
    found += countSolutions(next, limit - found);
    if (found >= limit) return found;
  }
  return found;
}
Enter fullscreen mode Exit fullscreen mode

If countSolutions(grid, 2) !== 1, the generator tweaks cell values or mutates a black-cell symmetry pair and re-checks.


3. Grading Difficulty by Human Logic, Not Grid Dimensions

A 6×6 puzzle can be brutally hard if none of its runs intersect cleanly, while a 10×10 grid packed with one-way sums (3=1+2, 17=8+9, 24=7+8+9) can be solved mechanically in minutes.

So instead of grading by board size, a separate human-style solver attempts to solve each unique grid using progressively stronger deduction tiers:

  • Easy: Only one-way sums and single-candidate intersections.
  • Medium: Cross-run candidate elimination (narrowing combinations across intersecting runs).
  • Hard: Subset/naked-pair eliminations and multi-run sum parity.
  • Expert: Longer elimination chains across 3+ intersecting runs.
  • Fiendish: Requires bifurcation (trial and contradiction).

The highest technique tier required to finish the puzzle determines its published grade.


4. Clean Black-and-White Printing

One surprise while building the Printable Kakuro sheet generator: browsers love dropping cell background shading unless print-color-adjust: exact is set, and hairline borders look jagged across A4 vs. US Letter scaling. Rendering the clue diagonals as inline SVG triangles inside crisp CSS grid cells fixed both issues cleanly and kept page weights tiny (~39 KB HTML).

Happy to answer questions about the bitmask propagation, uniqueness pruning, or print CSS tricks!

Top comments (0)