<?xml version="1.0" encoding="utf-8" standalone="yes"?><?xml-stylesheet type="text/xsl" href="/rss.xsl"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:glthr="https://glthr.com/ns/rss"><channel><title>Jane Street Puzzles | GLTHR.com, by Guillaume Lethuillier</title><link>https://glthr.com/series/jane-street-puzzles</link><description>Articles in Jane Street Puzzles on glthr.com.</description><language>en</language><lastBuildDate>Sun, 13 Oct 2024 15:51:43 +0000</lastBuildDate><atom:link href="https://glthr.com/series/jane-street-puzzles/" rel="self" type="application/rss+xml"/><item><title>Solving “Knight Moves 6” (Jane Street Puzzle, October 2024)</title><link>https://glthr.com/solving-knight-moves-6-jane-street-puzzle-october-2024</link><guid isPermaLink="true">https://glthr.com/solving-knight-moves-6-jane-street-puzzle-october-2024</guid><pubDate>Sun, 13 Oct 2024 15:51:43 +0000</pubDate><dc:creator>Guillaume Lethuillier</dc:creator><description>My solver for Jane Street’s “Knight Moves 6” reaches the minimal sum, 6 (A = 1, B = 3, C = 2), with two knight trips of 15 squares each that score 2024.</description><content:encoded><![CDATA[<aside class="callout callout-abstract">
  <span class="callout-emoji" aria-hidden="true">↘</span>
  <div class="callout-body">
    <p>In October 2024, Jane Street released <a href="https://www.janestreet.com/puzzles/knight-moves-6-index/" target="_blank" rel="noopener">“Knight Moves 6”</a> (<a href="https://www.janestreet.com/puzzles/knight-moves-6-solution/" target="_blank" rel="noopener">official solution</a>). My solver searches random legal knight trips and finds A = 1, B = 3, C = 2, with two trips that each score 2024. Their sum, 6, is minimal because A, B and C are distinct positive integers. The shortest pair my solver found for these values visits 15 squares per trip; I do not establish that no shorter pair exists.</p>
  </div>
</aside><h2 id="heading-the-puzzle">The Puzzle<a class="anchor" href="#heading-the-puzzle" aria-hidden="true" tabindex="-1">#</a></h2>
<p>This puzzle is straightforward. <a href="https://www.janestreet.com/puzzles/knight-moves-6-index/" target="_blank" rel="noopener">As explained on the webpage</a>, a 6x6 chessboard has squares labeled from A to C, corresponding to positive integers. The puzzle is solved by assigning values to A, B, and C and creating two corner-to-corner trips using knight’s moves so that each trip scores 2024. The method to calculate the score is: a trip starts with A points; then <em>(a)</em> moving between two <em>different</em> integers <strong>multiplies</strong> the score by the value the knight is moving to, and <em>(b)</em> moving between two <em>identical</em> integers <strong>increments</strong> the score by the value the knight is moving to. Last constraint: a square can only be visited once per trip (but a square does not necessarily have to be visited).</p>
<p>The official webpage distinguishes between optimal trips and trips which, while not optimal, are qualifying (<em>i.e.</em>, A + B + C &lt; 50).</p>
<h2 id="heading-the-solver">The Solver<a class="anchor" href="#heading-the-solver" aria-hidden="true" tabindex="-1">#</a></h2>
<p>To solve the puzzle, I developed a solver that generates random legal knight moves for each trip. The solver also produces combinations of ABC values that satisfy the constraint A + B + C &lt; 50. It calculates the score corresponding to each combination of values for each path. When a score equals 2024, the solver verifies whether an opposing trip exists with the same values and score. If not, the trip is retained in memory until an opposing trip is found with the same values. However, if an opposing trip does exist, the two trips are evaluated against the global best score. If the total score is the lowest recorded, the combination of possible values is updated to ensure that their sum cannot exceed the best sum during the next exploration, thereby facilitating a rapid convergence toward the optimal solution.</p>
<h2 id="heading-results">Results<a class="anchor" href="#heading-results" aria-hidden="true" tabindex="-1">#</a></h2>
<p>The best values give <strong>the minimal sum, 6</strong> (A: 1, B: 3, C: 2): A, B and C are distinct positive integers, so their sum cannot be lower than 1 + 2 + 3. For these values, <strong>the shortest pair of trips that the solver found visits 30 squares</strong>, 15 per trip, as in this example, rendered by the solver:</p>
<div class="ix" data-ix="knight-player" id="ix-0">
<div data-ix-fallback><p><a class="img-link" href="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-6-30-squares.w.png" title="Open full size (2400×1533)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-6-30-squares.w-1800w.png" alt="A 6 by 6 board with files a to f and ranks 1 to 6, in three regions: A on the left, B in a band through the middle, C on the right, with A = 1, B = 3 and C = 2, sum 6. Two knight trips cross it, 30 squares in all, each scoring 2024. The solid blue trip, 15 squares, goes a1, b3, c1, e2, d4, b5, d6, e4, f2, d1, b2, c4, b6, d5, f6. The dashed rust trip, 15 squares, goes a6, c5, e4, f2, d3, e1, f3, e5, c6, d4, c2, a3, c4, e3, f1. Both trips make the move e4 to f2." width="1800" height="1150" data-full="/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-6-30-squares.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
</div>

</div>




<p>The solver has also identified several other solutions, with larger sums or longer trips, as depicted below:</p>
<div class="table-wrap" tabindex="0" role="region" aria-label="Scrollable table">
<table>
  <thead>
      <tr>
          <th>A</th>
          <th>B</th>
          <th>C</th>
          <th>Sum</th>
          <th>Squares of the two trips</th>
          <th>What it shows</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>1</td>
          <td>3</td>
          <td>2</td>
          <td>6</td>
          <td>15 + 15 = 30</td>
          <td>Minimal sum, shortest pair found (above)</td>
      </tr>
      <tr>
          <td>3</td>
          <td>1</td>
          <td>2</td>
          <td>6</td>
          <td>21 + 27 = 48</td>
          <td>Minimal sum with other values, longer trips</td>
      </tr>
      <tr>
          <td>6</td>
          <td>1</td>
          <td>2</td>
          <td>9</td>
          <td>15 + 15 = 30</td>
          <td>Same length, larger sum</td>
      </tr>
      <tr>
          <td>2</td>
          <td>1</td>
          <td>8</td>
          <td>11</td>
          <td>19 + 23 = 42</td>
          <td></td>
      </tr>
      <tr>
          <td>2</td>
          <td>1</td>
          <td>22</td>
          <td>25</td>
          <td>9 + 11 = 20</td>
          <td>Short trips need a large C</td>
      </tr>
      <tr>
          <td>3</td>
          <td>2</td>
          <td>44</td>
          <td>49</td>
          <td>11 + 9 = 20</td>
          <td>Just under the limit of 50</td>
      </tr>
  </tbody>
</table>
</div>
<p><a class="img-link" href="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-49-20-squares.w.png" title="Open full size (2400×1533)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-49-20-squares.w-1800w.png" alt="A 6 by 6 board with files a to f and ranks 1 to 6, in three regions: A on the left, B in a band through the middle, C on the right, with A = 3, B = 2 and C = 44, sum 49. Two knight trips cross it, 20 squares in all, each scoring 2024. The solid blue trip, 11 squares, goes a1, b3, d2, b1, a3, c2, d4, e6, f4, d5, f6. The dashed rust trip, 9 squares, goes a6, b4, c2, a3, c4, b6, d5, e3, f1. Both trips use the move between a3 and c2, in opposite directions." width="1800" height="1150" data-full="/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-49-20-squares.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p><a class="img-link" href="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-25-20-squares.w.png" title="Open full size (2400×1533)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-25-20-squares.w-1800w.png" alt="A 6 by 6 board with files a to f and ranks 1 to 6, in three regions: A on the left, B in a band through the middle, C on the right, with A = 2, B = 1 and C = 22, sum 25. Two knight trips cross it, 20 squares in all, each scoring 2024. The solid blue trip, 9 squares, goes a1, c2, d4, e2, c3, d1, f2, e4, f6. The dashed rust trip, 11 squares, goes a6, b4, d3, c5, e4, d2, c4, d6, f5, e3, f1." width="1800" height="1150" data-full="/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-25-20-squares.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p><a class="img-link" href="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-11-42-squares.w.png" title="Open full size (2400×1533)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-11-42-squares.w-1800w.png" alt="A 6 by 6 board with files a to f and ranks 1 to 6, in three regions: A on the left, B in a band through the middle, C on the right, with A = 2, B = 1 and C = 8, sum 11. Two knight trips cross it, 42 squares in all, each scoring 2024. The solid blue trip, 19 squares, goes a1, c2, a3, b5, c3, a4, c5, d3, b2, c4, e5, f3, d2, f1, e3, f5, d6, e4, f6. The dashed rust trip, 23 squares, goes a6, c5, d3, b2, d1, f2, e4, d2, b1, a3, c2, a1, b3, c1, a2, b4, c6, a5, c4, b6, d5, e3, f1. Both trips use the moves between a1 and c2, a3 and c2, b2 and d3, c5 and d3, e3 and f1, in either direction." width="1800" height="1150" data-full="/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-11-42-squares.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p><a class="img-link" href="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-9-30-squares.w.png" title="Open full size (2400×1533)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-9-30-squares.w-1800w.png" alt="A 6 by 6 board with files a to f and ranks 1 to 6, in three regions: A on the left, B in a band through the middle, C on the right, with A = 6, B = 1 and C = 2, sum 9. Two knight trips cross it, 30 squares in all, each scoring 2024. The solid blue trip, 15 squares, goes a1, c2, e1, d3, b2, a4, c3, b1, d2, f1, e3, c4, b6, d5, f6. The dashed rust trip, 15 squares, goes a6, b4, d5, b6, c4, a5, b3, c5, a4, c3, e2, d4, f3, d2, f1. Both trips use the moves between a4 and c3, b6 and c4, b6 and d5, d2 and f1, in either direction." width="1800" height="1150" data-full="/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-9-30-squares.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p><a class="img-link" href="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-6-48-squares.w.png" title="Open full size (2400×1533)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-6-48-squares.w-1800w.png" alt="A 6 by 6 board with files a to f and ranks 1 to 6, in three regions: A on the left, B in a band through the middle, C on the right, with A = 3, B = 1 and C = 2, sum 6. Two knight trips cross it, 48 squares in all, each scoring 2024. The solid blue trip, 21 squares, goes a1, c2, d4, e6, f4, d5, b6, a4, c3, b5, a3, b1, d2, b3, c5, d3, e5, c4, d6, e4, f6. The dashed rust trip, 27 squares, goes a6, c5, e6, f4, d5, b4, c6, d4, f5, e3, d1, f2, e4, c3, a4, b2, c4, a5, b3, a1, c2, e1, d3, e5, f3, d2, f1. Both trips use the moves between a1 and c2, a4 and c3, d3 and e5, d5 and f4, e6 and f4, in either direction." width="1800" height="1150" data-full="/images/solving-knight-moves-6-jane-street-puzzle-october-2024/trips-sum-6-48-squares.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
]]></content:encoded></item><item><title>Solving “Number Cross 4” (Jane Street Puzzle, May 2024)</title><link>https://glthr.com/solving-number-cross-4-jane-street-puzzle-may-2024</link><guid isPermaLink="true">https://glthr.com/solving-number-cross-4-jane-street-puzzle-may-2024</guid><pubDate>Mon, 03 Jun 2024 15:35:24 +0000</pubDate><dc:creator>Guillaume Lethuillier</dc:creator><description>Walkthrough and unique solution (88243711283) for Jane Street’s May 2024 “Number Cross 4” puzzle using a row-by-row Go solver with region relabelling.</description><content:encoded><![CDATA[<aside class="callout callout-abstract">
  <span class="callout-emoji" aria-hidden="true">↘</span>
  <div class="callout-body">
    <p>In May 2024, Jane Street released a cross-number puzzle, <a href="https://www.janestreet.com/puzzles/number-cross-4-index/" target="_blank" rel="noopener"><strong>“Number Cross 4”</strong></a> (<a href="https://www.janestreet.com/puzzles/number-cross-4-solution/" target="_blank" rel="noopener">official solution</a>). My Go solver found its unique solution, <code>88243711283</code>, by building the 11-by-11 grid row by row; what makes this possible is relabelling the regions that shaded cells may split.</p>
  </div>
</aside><h2 id="heading-introduction">Introduction<a class="anchor" href="#heading-introduction" aria-hidden="true" tabindex="-1">#</a></h2>
<p>This puzzle, an 11-by-11 grid described on <a href="https://www.janestreet.com/puzzles/number-cross-4-index/" target="_blank" rel="noopener">Jane Street’s “Number Cross 4” page</a>, encompasses various types of constraints:</p>
<ul>
<li>
<p><strong>Row clues</strong> with which numbers, formed by concatenating adjacent digits from non-shaded cells, must comply (<em>e.g.</em>, square numbers, palindromic numbers).</p>
</li>
<li>
<p><strong>Rules governing the relationship between digits and regions</strong>: every cell within a region must contain the same digit, and orthogonally adjacent cells in different regions must contain different digits.</p>
</li>
<li>
<p><strong>Locations of shaded cells</strong>, which need to be sparse and not result in single-digit numbers.</p>
</li>
</ul>
<p><strong>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.</strong></p>
<h2 id="heading-solution">Solution<a class="anchor" href="#heading-solution" aria-hidden="true" tabindex="-1">#</a></h2>
<p>I submitted the unique solution, <code>88243711283</code> (the sum of all the numbers), discovered by the solver on May 12.</p>
<p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/solved-grid.w.png" title="Open full size (2400×1320)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/solved-grid.w-1800w.png" alt="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." width="1800" height="990" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/solved-grid.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<h2 id="heading-initial-failed-attempt-use-of-an-smt-solver">Initial Failed Attempt: Use of an SMT Solver<a class="anchor" href="#heading-initial-failed-attempt-use-of-an-smt-solver" aria-hidden="true" tabindex="-1">#</a></h2>
<p>I initially attempted to solve the puzzle by modeling it as an SMT problem to leverage the <a href="https://github.com/Z3Prover/z3" target="_blank" rel="noopener">Z3 solver</a>.</p>
<p>However, this strategy proved unsuccessful for several reasons:</p>
<ol>
<li>
<p>My solver is implemented in Go, and <a href="https://pkg.go.dev/github.com/mitchellh/go-z3" target="_blank" rel="noopener">the main Z3 library available for this language</a> is <a href="https://github.com/mitchellh/go-z3" target="_blank" rel="noopener">no longer maintained</a>, leading to frequent crashes. Additionally, this library does not handle parallel calls well, making it unsuitable for concurrent operations.</p>
</li>
<li>
<p>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?</p>
</li>
<li>
<p>The solving process was slower than expected for easily modelable clues (<em>e.g.</em>, generating square numbers), making it unfit to tackle the puzzle as a whole.</p>
</li>
</ol>
<p>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. <strong>I chose an iterative solving approach, where:</strong></p>
<ul>
<li>
<p>(identification) <strong>each row satisfying the constraints is identified</strong>, then</p>
</li>
<li>
<p>(augmentation) <strong>added to rows that are above it and that satisfy their constraints</strong>, and</p>
</li>
<li>
<p>(propagation) <strong>alongside its own constraints (digits assigned to regions potentially connected to the next row), propagated to a new instance of the solver</strong> (that is: a goroutine).</p>
</li>
</ul>
<h2 id="heading-successful-approach-solve-and-propagate-valid-rows">Successful Approach: Solve and Propagate Valid Rows<a class="anchor" href="#heading-successful-approach-solve-and-propagate-valid-rows" aria-hidden="true" tabindex="-1">#</a></h2>
<p>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.</p>
<div class="ix" data-ix="number-cross-output" id="ix-0">
<div data-ix-fallback><div class="table-wrap" tabindex="0" role="region" aria-label="Scrollable table">
<table>
  <thead>
      <tr>
          <th>Regions</th>
          <th>Digits</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>aaabbbccddd</code></td>
          <td><code>11122233444</code></td>
      </tr>
      <tr>
          <td><code>aeeeb*cddd*</code></td>
          <td><code>13332*3444*</code></td>
      </tr>
      <tr>
          <td><code>aeef*gcdddh</code></td>
          <td><code>1331*734449</code></td>
      </tr>
      <tr>
          <td><code>aee*ijjdkk*</code></td>
          <td><code>133*100411*</code></td>
      </tr>
      <tr>
          <td><code>ae*lmm*dkno</code></td>
          <td><code>13*144*4181</code></td>
      </tr>
      <tr>
          <td><code>appp*dddnnq</code></td>
          <td><code>1444*444889</code></td>
      </tr>
      <tr>
          <td><code>rpppp*sdnnn</code></td>
          <td><code>74444*74888</code></td>
      </tr>
      <tr>
          <td><code>rrtpuss*vnw</code></td>
          <td><code>7714177*989</code></td>
      </tr>
      <tr>
          <td><code>rruuusswwww</code></td>
          <td><code>77111779999</code></td>
      </tr>
      <tr>
          <td><code>*uuxx*swwwy</code></td>
          <td><code>*1144*79992</code></td>
      </tr>
      <tr>
          <td><code>xxxxxz*@wwy</code></td>
          <td><code>444443*3992</code></td>
      </tr>
  </tbody>
</table>
</div>
<p><em>The solver’s final region labels and digits. An asterisk marks a shaded cell.</em></p>
</div>

</div>




<p><strong>A significant factor contributing to this efficiency is how the puzzle’s constraints are constructed, enabling the solver to converge quickly toward the solution</strong>. 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).</p>
<p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/search-tree.w.png" title="Open full size (2400×2280)"><img class="has-dark" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/search-tree.w-1800w.png" alt="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." width="1800" height="1710" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/search-tree.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p>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.</p>
<h3 id="heading-solving-row-by-row">Solving Row by Row<a class="anchor" href="#heading-solving-row-by-row" aria-hidden="true" tabindex="-1">#</a></h3>
<p><strong>The initial challenge was determining whether this puzzle could be solved iteratively, row by row <em>(from top to bottom)</em>, or if a more global approach was necessary due to interdependencies between cells from different rows.</strong></p>
<p>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 <em>and, critically, below</em>, as demonstrated in the following illustration:</p>
<p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/regions-row-below.w.png" title="Open full size (2400×1140)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/regions-row-below.w-1800w.png" alt="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′." width="1800" height="855" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/regions-row-below.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p>In this example, the current row (indicated by the arrow) may ultimately have <em>three</em> (<code>A</code>, <code>B</code>, and <code>C</code>) or <em>four</em> (<code>A</code>, <code>B</code>, <code>C</code>, and <code>C’</code>)—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 <code>C</code> cells. This information cannot be determined when the current row is being solved: <em>it cannot be decided yet, as it depends on the placement of shaded cells on a row that has not been solved yet</em>.</p>
<p>Upon further consideration, I discovered that the originally joined regions could be <em>temporarily</em> 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 <em>(a)</em> a top row is not connected to them and <em>(b)</em> they are separated by a region or a shaded cell in the current row. In this example, the original regions <code>A</code>, <code>B</code>, <code>C</code>, become <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>.</p>
<p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/relabel-regions.w.png" title="Open full size (2400×495)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/relabel-regions.w-1800w.png" alt="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." width="1800" height="371" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/relabel-regions.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p>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.</p>
<p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/digit-checks.w.png" title="Open full size (2400×630)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/digit-checks.w-1800w.png" alt="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." width="1800" height="473" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/digit-checks.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
<p><strong>This insight supported the idea that the puzzle could be solved on a row-by-row basis.</strong></p>
<p>Before discussing how the solver traverses the search space, I must first clarify the generation of shaded cells and how regions are encoded.</p>
<h3 id="heading-generating-the-shaded-cells">Generating the Shaded Cells<a class="anchor" href="#heading-generating-the-shaded-cells" aria-hidden="true" tabindex="-1">#</a></h3>
<p>The solver represents each configuration of shaded cells as a <em>mask</em> (a bitfield; a slice of booleans) where an unset bit <code>0</code> represents a digit, and a set bit <code>1</code> represents a shaded cell. For example, <code>00100000100</code> corresponds to three groups of digits and two shaded cells: one at x-position 2 and another at x-position 8.</p>
<p>Valid masks must comply with these rules:</p>
<ol>
<li>
<p>Unset bits cannot be isolated (that is: <code>01---</code>, <code>---101---</code>, or <code>---10</code>) as the numbers must be at least 2 digits long.</p>
</li>
<li>
<p>Set bits cannot be horizontally adjacent as shaded cells are horizontally sparse (<em>i.e.</em>, <code>---11---</code>).</p>
</li>
<li>
<p>Set bits cannot be vertically adjacent as shaded cells are also vertically sparse (<em>e.g.</em>, top row <code>0010010</code> is incompatible with the bottom row <code>0010000</code> because of the shaded cells at x-position 2).</p>
</li>
</ol>
<p>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).</p>
<p>This function generates 54 valid masks per row.</p>
<h3 id="heading-encoding-the-regions">Encoding the Regions<a class="anchor" href="#heading-encoding-the-regions" aria-hidden="true" tabindex="-1">#</a></h3>
<p>The original regions are defined in a configuration file and denoted using uppercase letters, ranging from <code>A</code> to <code>M</code>. 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 (<code>a</code> to <code>z</code>, <em>etc.</em>).</p>
<p>Regions are updated as follows:</p>
<ul>
<li>
<p>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.</p>
<div class="ix" data-ix="regions-carry" id="ix-1">
<div data-ix-fallback><p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/carry-down.w.png" title="Open full size (2400×435)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/carry-down.w-1800w.png" alt="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." width="1800" height="326" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/carry-down.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
</div>

</div>


</li>
<li>
<p>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.</p>
<p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/new-symbols.w.png" title="Open full size (2400×495)"><img class="has-dark img-wide" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/new-symbols.w-1800w.png" alt="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." width="1800" height="371" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/new-symbols.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
</li>
</ul>
<h2 id="heading-solving-process">Solving Process<a class="anchor" href="#heading-solving-process" aria-hidden="true" tabindex="-1">#</a></h2>
<p>My solver proceeds like this:</p>
<div class="ix" data-ix="solver-flow" id="ix-2">
<div data-ix-fallback><p><a class="img-link" href="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/solver-flowchart.w.png" title="Open full size (2400×2220)"><img class="has-dark" src="https://glthr.com/images/solving-number-cross-4-jane-street-puzzle-may-2024/solver-flowchart.w-1800w.png" alt="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." width="1800" height="1665" data-full="/images/solving-number-cross-4-jane-street-puzzle-may-2024/solver-flowchart.w.png" data-full-width="2400" loading="lazy" decoding="async"></a></p>
</div>

</div>


<details class="fold">
  <summary>The fourteen steps, one by one</summary>
  <div class="fold-body">
    <ul>
<li>
<p><em>(a)</em> The solving function, initiated in a goroutine, begins by examining a prospective candidate containing 0 to 11 valid rows.</p>
</li>
<li>
<p><em>(b)</em> If the candidate contains 11 rows, terminate the present exploration since the candidate is a solution. <em>(Solved).</em></p>
</li>
<li>
<p><em>(c)</em> Otherwise, iterate through all viable arrangements of shaded cells (as described above).</p>
</li>
<li>
<p><em>(d)</em> If no such configurations remain, exit the current process: the candidate cannot possibly yield a solution.</p>
</li>
<li>
<p><em>(e)</em> Compare the present configuration of shaded cells with the one in the row above. If two shaded cells are vertically adjacent, return to step <em>(c)</em>, as vertical sparsity is required for shaded cells.</p>
</li>
<li>
<p><em>(f)</em> Instantiate the current row based on the original regions and the current mask.</p>
</li>
<li>
<p><em>(g)</em> 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).</p>
</li>
<li>
<p><em>(h)</em> 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.</p>
</li>
<li>
<p><em>(i)</em> If no new Cartesian product can be generated, go back to <em>(c)</em> to try a new mask.</p>
</li>
<li>
<p><em>(j)</em> If the generated digits result in at least one number starting with zero, revert to step <em>(h)</em> until all numbers start with nonzero digits.</p>
</li>
<li>
<p><em>(k)</em> 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 <em>(h)</em> until they meet this condition.</p>
</li>
<li>
<p><em>(l)</em> Ensure that the numbers comply with the row clue. If not, revert to step <em>(h)</em>.</p>
</li>
<li>
<p><em>(m)</em> At this stage, the row satisfies all constraints (regarding regions, digits, and clues). Consequently, the candidate can be augmented with this new row.</p>
</li>
<li>
<p><em>(n)</em> A new solving goroutine is subsequently spawned to try solving the augmented candidate, starting from step <em>(a)</em>.</p>
</li>
</ul>
  </div>
</details><h2 id="heading-conclusion">Conclusion<a class="anchor" href="#heading-conclusion" aria-hidden="true" tabindex="-1">#</a></h2>
<p>While solving this puzzle, I came to the conclusion that:</p>
<ul>
<li>
<p>The current Z3 library for Go is suboptimal. I would love to see a more comprehensive implementation for Go.</p>
</li>
<li>
<p>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.</p>
</li>
<li>
<p>I implemented a multithreaded solver because Go makes it so convenient to use goroutines, but, in retrospect, it was unnecessary.</p>
</li>
<li>
<p>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.</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>