Solving “Number Cross 4” (Jane Street Puzzle, May 2024)

A Go Solver That Relabels Split Regions to Solve the Grid Row by Row

Introduction

This puzzle, an 11-by-11 grid described on Jane Street’s “Number Cross 4” page, encompasses various types of constraints:

  • Row clues with which numbers, formed by concatenating adjacent digits from non-shaded cells, must comply (e.g., square numbers, palindromic numbers).

  • Rules governing the relationship between digits and regions: every cell within a region must contain the same digit, and orthogonally adjacent cells in different regions must contain different digits.

  • Locations of shaded cells, which need to be sparse and not result in single-digit numbers.

The difficulty of this multi-constraint puzzle comes from the fact that regions are ultimately defined by where the solver more or less arbitrarily places shaded cells.

Solution

I submitted the unique solution, 88243711283 (the sum of all the numbers), discovered by the solver on May 12.

The solved 11 by 11 grid: digits in cells with thick region borders and hatched shaded cells. Each row’s clue is on its left and, on its right, how its numbers satisfy it: square, 105462 squared; 1 more than a palindrome, 13331 + 1 and 3443 + 1; prime raised to a prime power, 11 cubed and 857 squared; sum of digits is 7, 1+3+3 and 1+0+0+4+1+1; fibonacci, F7, F12 and F19; square, 38 squared and 667 squared; multiple of 37, 37 × 2012 and 37 × 2024; palindrome and multiple of 23, 23 × 335399 and 23 × 43; product of digits ends in 1, product 15752961; multiple of 88, 88 × 13 and 88 × 909; 1 less than a palindrome, 444444 − 1 and 3993 − 1.The solved 11 by 11 grid: digits in cells with thick region borders and hatched shaded cells. Each row’s clue is on its left and, on its right, how its numbers satisfy it: square, 105462 squared; 1 more than a palindrome, 13331 + 1 and 3443 + 1; prime raised to a prime power, 11 cubed and 857 squared; sum of digits is 7, 1+3+3 and 1+0+0+4+1+1; fibonacci, F7, F12 and F19; square, 38 squared and 667 squared; multiple of 37, 37 × 2012 and 37 × 2024; palindrome and multiple of 23, 23 × 335399 and 23 × 43; product of digits ends in 1, product 15752961; multiple of 88, 88 × 13 and 88 × 909; 1 less than a palindrome, 444444 − 1 and 3993 − 1.

Initial Failed Attempt: Use of an SMT Solver

I initially attempted to solve the puzzle by modeling it as an SMT problem to leverage the Z3 solver.

However, this strategy proved unsuccessful for several reasons:

  1. My solver is implemented in Go, and the main Z3 library available for this language is no longer maintained, leading to frequent crashes. Additionally, this library does not handle parallel calls well, making it unsuitable for concurrent operations.

  2. Some of the clues posed significant challenges when modeled under these conditions. For example, how can digits be restricted to form one or more primes raised to a prime power (3rd-row clue)? More generally, how can the relationship between shaded cells, regions, and numbers be modeled?

  3. The solving process was slower than expected for easily modelable clues (e.g., generating square numbers), making it unfit to tackle the puzzle as a whole.

Due to these outcomes—probably not because of Z3 itself, but rather how I used it and the limitations of the Go library—I took a different approach. I chose an iterative solving approach, where:

  • (identification) each row satisfying the constraints is identified, then

  • (augmentation) added to rows that are above it and that satisfy their constraints, and

  • (propagation) alongside its own constraints (digits assigned to regions potentially connected to the next row), propagated to a new instance of the solver (that is: a goroutine).

Successful Approach: Solve and Propagate Valid Rows

This second approach (essentially brute forcing but propagating the candidates forward instead of backtracking) proved successful. On a recent laptop, the solver—implementing this approach—traverses the entire valid (in terms of constraints) search space within approximately five seconds and finds the solution in about one second.

RegionsDigits
aaabbbccddd11122233444
aeeeb*cddd*13332*3444*
aeef*gcdddh1331*734449
aee*ijjdkk*133*100411*
ae*lmm*dkno13*144*4181
appp*dddnnq1444*444889
rpppp*sdnnn74444*74888
rrtpuss*vnw7714177*989
rruuusswwww77111779999
*uuxx*swwwy*1144*79992
xxxxxz*@wwy444443*3992

The solver’s final region labels and digits. An asterisk marks a shaded cell.

A significant factor contributing to this efficiency is how the puzzle’s constraints are constructed, enabling the solver to converge quickly toward the solution. The following diagram illustrates this by showing the number of valid candidates at each level: the initial level (first row) generates just six eligible candidates. The subsequent level produces 816 such candidates. Subsequently, however, the number of valid candidates merely fluctuates between 1 (for four levels) and 8 (for one level).

A row-by-row search tree with four aligned columns: row number, candidate count, the green path to the unique solution, and discarded candidates in dashed grey boxes. Rows 0 through 10 have 6, 816, 1, 2, 3, 1, 8, 1, 2, 2 and 1 candidates. The successful row strings form a straight vertical spine linked by next-row arrows. The five other first-row candidates lead to 815 other partial grids at row 1; no alternative reaches row 2. Later alternatives terminate before the solution, whose last row is 444443 followed by a shaded cell and 3992. An asterisk marks a shaded cell.A row-by-row search tree with four aligned columns: row number, candidate count, the green path to the unique solution, and discarded candidates in dashed grey boxes. Rows 0 through 10 have 6, 816, 1, 2, 3, 1, 8, 1, 2, 2 and 1 candidates. The successful row strings form a straight vertical spine linked by next-row arrows. The five other first-row candidates lead to 815 other partial grids at row 1; no alternative reaches row 2. Later alternatives terminate before the solution, whose last row is 444443 followed by a shaded cell and 3992. An asterisk marks a shaded cell.

For this reason, a single-threaded implementation of this solver takes less than 30 seconds on similar hardware to solve the puzzle. Thus, in retrospect, it appears that parallelism is not crucial to solving this puzzle efficiently.

Solving Row by Row

The initial challenge was determining whether this puzzle could be solved iteratively, row by row (from top to bottom), or if a more global approach was necessary due to interdependencies between cells from different rows.

Initially, my intuition leaned towards requiring a global approach, given that the definition of regions in a row depends on the shaded cells in the rows above and, critically, below, as demonstrated in the following illustration:

Top: three rows of six cells. Row above A A B B B C; current row, marked, A B B C B C; row below unknown, six question marks. Two outcomes. If the C cells are joined below, the row below reads A B B C C C and the current row has 3 regions: A, B, C. If a shaded cell splits them, the row below reads A B B C′, shaded, C; the isolated C becomes C′ and there are 4 regions: A, B, C, C′.Top: three rows of six cells. Row above A A B B B C; current row, marked, A B B C B C; row below unknown, six question marks. Two outcomes. If the C cells are joined below, the row below reads A B B C C C and the current row has 3 regions: A, B, C. If a shaded cell splits them, the row below reads A B B C′, shaded, C; the isolated C becomes C′ and there are 4 regions: A, B, C, C′.

In this example, the current row (indicated by the arrow) may ultimately have three (A, B, and C) or four (A, B, C, and C’)—or even more—regions based on the presence of a shaded cell at a specific position within the row below that separates (or not) the C cells. This information cannot be determined when the current row is being solved: it cannot be decided yet, as it depends on the placement of shaded cells on a row that has not been solved yet.

Upon further consideration, I discovered that the originally joined regions could be temporarily considered disjoint at a row level, meaning they should not necessarily have the same digits. In other words, cells originally belonging to the same region could be assigned to different regions when (a) a top row is not connected to them and (b) they are separated by a region or a shaded cell in the current row. In this example, the original regions A, B, C, become a, b, c, and d.

Two rows of six cells before and after relabelling. Original regions: row above A A B B B C, current row A B B C B C. Updated regions: row above a a b b b c, current row a b b d b c. The separated C cell of the current row becomes a new region, d.Two rows of six cells before and after relabelling. Original regions: row above A A B B B C, current row A B B C B C. Updated regions: row above a a b b b c, current row a b b d b c. The separated C cell of the current row becomes a new region, d.

That way, if they are actually connected by the row below (once it is the turn for the solver to consider the row below), any digit assignment that would violate the rule according to which regions should have the same digits would be excluded. Conversely, if they stay disjoint, nothing ties their digits: the two parts are never orthogonally adjacent, so they may hold the same digit or different ones.

Three digit assignments on three rows, side by side. Joined below, rejected: the current row’s separated cell holds 4 but is joined from below to cells holding 3, so one region would hold 4 and 3. Joined below, accepted: the same cell holds 3, so the region holds one digit, 3. Split below, accepted: a shaded cell in the row below separates the two regions, which hold 4 and 3, each carried down to the row below.Three digit assignments on three rows, side by side. Joined below, rejected: the current row’s separated cell holds 4 but is joined from below to cells holding 3, so one region would hold 4 and 3. Joined below, accepted: the same cell holds 3, so the region holds one digit, 3. Split below, accepted: a shaded cell in the row below separates the two regions, which hold 4 and 3, each carried down to the row below.

This insight supported the idea that the puzzle could be solved on a row-by-row basis.

Before discussing how the solver traverses the search space, I must first clarify the generation of shaded cells and how regions are encoded.

Generating the Shaded Cells

The solver represents each configuration of shaded cells as a mask (a bitfield; a slice of booleans) where an unset bit 0 represents a digit, and a set bit 1 represents a shaded cell. For example, 00100000100 corresponds to three groups of digits and two shaded cells: one at x-position 2 and another at x-position 8.

Valid masks must comply with these rules:

  1. Unset bits cannot be isolated (that is: 01---, ---101---, or ---10) as the numbers must be at least 2 digits long.

  2. Set bits cannot be horizontally adjacent as shaded cells are horizontally sparse (i.e., ---11---).

  3. Set bits cannot be vertically adjacent as shaded cells are also vertically sparse (e.g., top row 0010010 is incompatible with the bottom row 0010000 because of the shaded cells at x-position 2).

The masks are trivially generated by a function that builds the Cartesian product of sets of set and unset bits and filters out masks that do not satisfy the first two rules (the third one—vertical sparsity—can only be enforced, by a different function, between two rows).

This function generates 54 valid masks per row.

Encoding the Regions

The original regions are defined in a configuration file and denoted using uppercase letters, ranging from A to M. Updated regions are defined row-by-row by updating the original regions based on a given configuration of shaded cells—as explained above. These regions are symbolized by characters that have not yet been utilized to represent the original regions (a to z, etc.).

Regions are updated as follows:

  • Initially, when cells in the current row are connected with those in the preceding row—by considering the original regions—the updated symbols representing these regions in the upper row are carried down to the lower row.

    Three stages of two rows of six cells. Original regions: row above A A B B B C, current row A B B B C C. Shaded cells added: row above a a b b b c; current row A, B, shaded, B, C, shaded, the capitals faint because these cells are not yet updated. Symbols carried down: arrows bring a to the first cell and b to the fourth, which are connected to the row above; the B and C cells are not yet assigned.Three stages of two rows of six cells. Original regions: row above A A B B B C, current row A B B B C C. Shaded cells added: row above a a b b b c; current row A, B, shaded, B, C, shaded, the capitals faint because these cells are not yet updated. Symbols carried down: arrows bring a to the first cell and b to the fourth, which are connected to the row above; the B and C cells are not yet assigned.

  • Subsequently, unassigned regions are assigned symbols not used so far. Cells belonging to the same original region and adjacent to each other are allocated the same symbol. They belong to distinct regions if originally connected but horizontally separated by a region or a shaded cell. This does not preclude them from possessing identical digits if it is subsequently determined that they are connected from below.

    Two rows of six cells before and after. Symbols carried down: row above a a b b b c; current row a, B, shaded, b, C, shaded. Updated regions: row above a a b b b c; current row a, d, shaded, b, e, shaded. The two cells still unassigned receive the new symbols d and e.Two rows of six cells before and after. Symbols carried down: row above a a b b b c; current row a, B, shaded, b, C, shaded. Updated regions: row above a a b b b c; current row a, d, shaded, b, e, shaded. The two cells still unassigned receive the new symbols d and e.

Solving Process

My solver proceeds like this:

Flowchart of the solver in two columns. Left: a, Candidate; a diamond, Contains less than 11 rows? No leads to b, Exit goroutine, solution found; Yes leads to c, Generate shaded cells; a diamond, New configuration of shaded cells generated? No leads to d, Exit goroutine, no solution found; Yes leads to e, Configuration compatible with top row? No returns to c; Yes leads to f, Generate current row based on shaded cells and original regions, then g, Generate regions for current row, which continues to the right column. Right: h, Propagate digits from top row and generate digits for current row regions; i, New configuration of digits generated? No returns to c; Yes leads to j, Numbers start with nonzero?, then k, Digits are congruent with the regions?, then l, Numbers satisfy the row clue?; a No at j, k or l returns to h. Yes at l leads to m, Augment candidate with current row, then n, New goroutine, which starts again at a. A note defines congruent: same region, same digit; adjacent regions, different digits; top regions joined from below, same digit.Flowchart of the solver in two columns. Left: a, Candidate; a diamond, Contains less than 11 rows? No leads to b, Exit goroutine, solution found; Yes leads to c, Generate shaded cells; a diamond, New configuration of shaded cells generated? No leads to d, Exit goroutine, no solution found; Yes leads to e, Configuration compatible with top row? No returns to c; Yes leads to f, Generate current row based on shaded cells and original regions, then g, Generate regions for current row, which continues to the right column. Right: h, Propagate digits from top row and generate digits for current row regions; i, New configuration of digits generated? No returns to c; Yes leads to j, Numbers start with nonzero?, then k, Digits are congruent with the regions?, then l, Numbers satisfy the row clue?; a No at j, k or l returns to h. Yes at l leads to m, Augment candidate with current row, then n, New goroutine, which starts again at a. A note defines congruent: same region, same digit; adjacent regions, different digits; top regions joined from below, same digit.

The fourteen steps, one by one
  • (a) The solving function, initiated in a goroutine, begins by examining a prospective candidate containing 0 to 11 valid rows.

  • (b) If the candidate contains 11 rows, terminate the present exploration since the candidate is a solution. (Solved).

  • (c) Otherwise, iterate through all viable arrangements of shaded cells (as described above).

  • (d) If no such configurations remain, exit the current process: the candidate cannot possibly yield a solution.

  • (e) Compare the present configuration of shaded cells with the one in the row above. If two shaded cells are vertically adjacent, return to step (c), as vertical sparsity is required for shaded cells.

  • (f) Instantiate the current row based on the original regions and the current mask.

  • (g) Generate the regions for the current row by first propagating the regions of interconnected cells from the row above and, secondly, creating new regions for the remaining cells (as described above).

  • (h) Reuse the digits used in the row above for connected regions. For the row’s own regions, generate the Cartesian product of the sets containing digits from 0 to 9 so that each new region is assigned a digit.

  • (i) If no new Cartesian product can be generated, go back to (c) to try a new mask.

  • (j) If the generated digits result in at least one number starting with zero, revert to step (h) until all numbers start with nonzero digits.

  • (k) Verify that digits from adjacent regions are orthogonally different, digits from the same region are identical, and that top row regions retrospectively connected to the current row are identical (see above). If not, return to step (h) until they meet this condition.

  • (l) Ensure that the numbers comply with the row clue. If not, revert to step (h).

  • (m) At this stage, the row satisfies all constraints (regarding regions, digits, and clues). Consequently, the candidate can be augmented with this new row.

  • (n) A new solving goroutine is subsequently spawned to try solving the augmented candidate, starting from step (a).

Conclusion

While solving this puzzle, I came to the conclusion that:

  • The current Z3 library for Go is suboptimal. I would love to see a more comprehensive implementation for Go.

  • It would also perhaps have been easier to use a language like Python with more robust SMT libraries. I believe some of the people who solved the puzzle may have used this approach.

  • I implemented a multithreaded solver because Go makes it so convenient to use goroutines, but, in retrospect, it was unnecessary.

  • Solving parts of the puzzle and propagating augmented candidates forward is a good alternative to the classic tree exploration and backtracking combination, as it simplifies the programmatic flow in some cases, especially in a multithreading context.