Hexakai

The Mathematics of Hexakai


Introduction

Hexakai is built upon a fascinating mathematical foundation. In this post, I'm going to canvas some of the avenues of mathematical investigation I took when creating Hexakai, giving a reasonably concise overview while balancing specificity with accessibility. I've included some mathematical notations and functions, but overall, this is about concept, not calculation, so an inability to read this notation will not take much away from the conceptual explanations offered. Some of these concepts, such as constraints, are directly useful to the gameplay experience. Other concepts, such as templates, are useful to me as the developer of the Hexakai apps. I've decided to publish this mathematical overview for those who are interested in mathematics - either generally or as they pertain to games, those who deep dive into their hobbies, and above all, those who are fundamentally curious.

Types of Boards

This post will refer to two types of boards:

Most of the analysis here will refer to complete boards.

Fundamental Properties of Hexakai

Hexakai isn't bound to a single board size. While the canonical board size is 10, boards can be constructed in sizes 3, 5, 7, and 8-16. Mathematically speaking, we can also construct a trivial board of size 1, and a board of every odd size, which we'll prove later in this post. My supposition is that we can also construct a board of any even size greater than 16, though I've not yet rigorously proved this is the case. Games of size 2, 4, and 6 are impossible to construct. This is a fascinating limitation that we'll dive into later in this post.

When a board's size is greater than 10, we'll typically use letters A, B, C, and so-on to represent values greater than 9, corresponding to 10, 11, 12, etc.

For the rest of this post, we'll use the variable n to refer to the size of a given game of Hexakai. When we discuss games of size n, the discussion will pertain to properties that hold for every possible board of Hexakai using n as a refernece point when the math comes into play.

These basic properties hold for all Hexakai boards:

Impossible Boards

It is impossible to construct any valid complete Hexakai boards of sizes 2, 4, or 6.

Size 2

This is a board of size 2, where each cell can have a value of 0 or 1. Once we set the top cell to a value of 0 or 1, we must set the leftmost cell to the other value. With those values set, what do we assign to the cell marked with an X?

The X cell is constrained by two other cells with two distinct values. We’ll call this the contradictory cell since any value we assign contradicts the rules of the game.

Regardless of which cells you assign first, or which values you assign to them, this situation will always arise with one of the cells on this board. Therefore, it is impossible to construct Hexakai boards of size 2.

Size 4

This is a board of size 4, where the value of each cell ranges from 0 to 3. First, we’ll assign 0, 1, and 2 to the top three cells. On the top-left diagonal, the next cell, marked "Y," can only have a value of 2 or 3, and on the top-right, the next cell, marked "Y," can only have a value of 1 or 3. The cell in the center of the third row shares a diagonal with the 1 and with the 2 above it, so it can only have a value of 0 or 3. We'll take those two cases in turn, starting with 0, which is the case shown on this board.

The problem is, which cell in the center row will be assigned a value of 0? It can’t be the leftmost or rightmost cell because they both include the topmost cell in their diagonals which already has a value of 0. It also can’t be the two middle cells, because they share a border with the cell marked 0 above, and that cell above has a value of 0. Therefore, the middle row cannot be assigned 0, and this case fails.

The other case is a 3 in the center of the third row. The two cells marked "Y" share a row with it, so neither can be 3: the left one must be 2 and the right one must be 1. The top-left edge of the board now holds 0, 1, and 2, so the leftmost cell of the middle row, which completes that diagonal, must be 3. The top-right edge holds 0, 2, and 1, so the rightmost cell of the middle row must also be 3. That puts two 3s in the same row, which the rules forbid. Both cases fail, and thus, it is impossible to construct the board.

Note that we started with values 0, 1, and 2 in this example, but the same problem will arise regardless of what values are set here. If we were to set the top cell to 1, then it would be impossible to assign 1 to any cell in the middle row, and the same holds for the other values. From a human perspective, we can tell there is an issue as soon as we hit the middle row, but if a computer were to attempt to create the board, it wouldn’t fail until it tried to assign the last cell of the middle row, as the first three cells can still receive values 1-3. This cell, which is the 10th cell of the board, is the contradictory cell for boards of size 4.

Size 6

This is a board of size 6, where the value of each cell ranges from 0 to 5. The proof of impossibility for boards of size 6 is significantly more complex than the previous proofs discussed. Instead of deriving it by hand, I modified my board generation algorithm to find exactly where the problem using the same top-down approach used for the analysis of the board of size 4. It found that the 28th cell, marked "X," is the contradictory cell. Note that the particular cell depends on the algorithm: one that fills cells in a different order, or tries values in a different order, may settle on a different cell. What doesn't change is that every algorithm will find one, because no complete board of size 6 exists.

If we look backward from the contradictory cell, we see that it is being constrained by 8 cells excluding cells that are below it or to its right: 4 from its diagonal up right, 3 from its diagonal up left, and 1 to its left. For this cell to be contradictory, every value 0-5 must already appear within those 8 cells. The diagonal up right contains 4 out of 6 of those values, so the remaining diagonal and cell to the left of the constraint cell contain the remaining two values.

Beyond Size 6

I haven't yet dedicated time to determining what higher-level mathematical constructs describe the root causes for the impossibility of the board sizes we've reviewed. From my experience writing the algorithm that generates Hexakai boards, I've observed that generating boards of even size is computationally more difficult than boards of odd sizes. Suprisingly, I can generate boards of size 11 faster than size 10, a phenomenon that usually doesn't take place due to the geometric growth of this type of algorithm. As we'll discuss later, a certain additional constraint that boards of size 5 and 7 can satisfy cannot be satisfied by boards of size 8 or 10, and the smallest even size that can satisfy it is 12.

I believe that there are no other "impossible" sizes beyond 6. For odd sizes this is proven in the next section, which constructs a valid board of every odd size. For even sizes I haven't mathematically proven it, but my supposition is supported by the following facts. In each impossible size beyond 2, the contradictory cell is closer to the bottom than in the previous impossible size. Furthermore, in general, there are more boards of size n than there are of boards $n-1$. Despite the observed higher level of difficulty in generating even sized boards, the number of possible boards still increases geometrically as n increases, implying that the additional space and values available allow us to satisfy the relationships imposed by the constraints in increasingly more ways. Therefore, I believe that beyond boards of size 6, there will always be multiple ways to satisfy the constraints and create valid complete boards.

Every Odd Size Is Possible

A complete board exists for every odd size, and we can prove it by writing one down. Consider the n diagonals that run down and to the right, starting with the one that begins at the topmost cell and moving down the board's left edge from there:

  1. Fill the first diagonal with the values in order: $0, 1, 2, \ldots, n-1$.
  2. Fill the second diagonal the same way, but start at 2 and wrap around: $2, 3, \ldots, n-1, 0, 1$.
  3. Fill the third diagonal starting at 4: $4, 5, \ldots, n-1, 0, 1, 2, 3$.
  4. Continue in the same manner, starting each diagonal 2 higher than the one before it and wrapping back around to 0 after $n-1$.

This board shows the result for a size of 5. The first diagonal reads 0, 1, 2, 3, 4 from the top cell, the second reads 2, 3, 4, 0, 1, the third reads 4, 0, 1, 2, 3, and so on.

To state this precisely, number these diagonals $a = 0, 1, \ldots, n-1$ and number the positions along each one $b = 0, 1, \ldots, n-1$. The value of the cell at position $b$ of diagonal $a$ is:

$$ \text{value}(a, b) = (2a + b) \bmod n $$

Here, $x \bmod n$ is the remainder when $x$ is divided by n, which is what wrapping back around to 0 means. The board is valid when n is odd because all three rules of the game hold:

This also shows why the same construction fails for every even size. The rows and the down-right diagonals are still fine, but the down-left diagonals are not: when n is even, stepping by 2 returns to its starting value after only $\frac{n}{2}$ steps, without visiting the values in between. For a size of 6, the steps from 0 are 0, 2, 4, and then 0 again, so every down-left diagonal would contain each of its values twice.

We might hope to rescue the idea for even sizes by starting each diagonal some other number of values higher than the one before it. Call that number the step, so that the construction above has a step of 2. For the down-left diagonals to contain every value, the step must share no factor with n other than 1. Otherwise it returns to its starting value too early: a step of 2 does so whenever n is even, and a step of 3 does so whenever n is divisible by 3. When n is even, every step that passes this test is odd. One step always passes it: $n - 1$, which amounts to going backward by one value on each diagonal.

The rows break instead. With a step of $k$, the value of a cell is $(ka + b) \bmod n$, which is $(k - 1)a$ plus the fixed number $a + b$ along a row, so the values along a row go up in steps of $k - 1$. An odd step makes $k - 1$ even, and stepping by an even amount through an even number of values returns to its start after at most $\frac{n}{2}$ steps. Any row with more than $\frac{n}{2}$ cells therefore repeats a value, and for sizes of 4 and above, such rows appear before the center row is reached. With the backward step, the first collision is in row $\frac{n}{2} + 1$. For a size of 8, the values are $(b - a) \bmod 8$, and the fifth row reads 4, 6, 0, 2, 4, with the same value at both ends.

The two requirements can never be met together. The down-left diagonals need the step $k$ to share no factor with n, and the center row, which is n cells long, needs $k - 1$ to share no factor with n. One of $k$ and $k - 1$ is always even, so when n is even, one of them always shares the factor 2 with it. I also checked this by computer for every even size up to 30, including the variations where the values along each diagonal go up by a step other than 1, and none of them produces a valid board.

The construction therefore proves that a complete Hexakai board exists for every odd size, no matter how large. It says nothing about even sizes in general, because it only shows that this family of methods fails for them. Even sizes of 8 and above still need another method, and so far the only evidence I have for them is that my algorithm has generated boards of every even size from 8 to 16.

Triangle Numbers

The concept of triangle numbers wasn't created specifically for Hexakai, but it is an integral component of its mathematics, so I'll dedicate this section to introducing it. A triangle number is a positive whole number that is derived from adding all the numbers $1 + 2 + \ldots + x$. For example, the third triangle number is $1 + 2 + 3 = 6$ and the xth triangle number is $1 + 2 + \ldots + x$. Conceptually speaking, a triangle number counts the number of building blocks in an equilateral triangle, a triangle whose side lengths are all the same, as exemplified in the diagram below.

This diagram visualizes the first five triangle numbers: 1, 3, 6, 10, and 15. The first triangle number is a trivial case, one dot. The second triangle number has two dots on the bottom, two dots on the left slope, and two dots on the right slope. The third triangle has three in each, the fourth has four, etc. Additionally, for each triangle, the top row has one dot, the next has two, then three, etc. The formula to derive the xth triangle number is $T(x) = \frac{x(x+1)}{2}$. We've actually just seen a slightly modified version of this in the section above when we discussed the sum of values in each diagonal.

A Hexakai board can be constructed by stacking two triangular arrangements of hexagons, a triangle of size $T(n)$ on the top, and an upside-down triangle of size $T(n-1)$ on the bottom (where n is the size of the Hexakai board). Triangle numbers appear frequently in calculations related to Hexakai regarding board sizes, value distributions, and even board generation algorithms.

Board Templates

A board template a Hexakai board whose cells are comprised of variables instead of values. For simplicity, variables will be denoted as lowercase letters. Templates play a crucial role in analyzing the mathematics of the game as well as implementing algorithms to generate games.

In the display below, the first board is a board template of a Hexakai game of size five (i.e., n=5) and the second board is a complete board whose variables have been substituted for values - a becomes 4, b becomes 1, etc. This process works both ways: given a board template, we can assign a value to each variable and derive a complete board, and given a complete board, we can replace values with variables and derive a template. We'll discuss that process in more detail shortly.

In a board template, the name of any given cell is the variable that occupies it. For example, the name of the topmost cell in the board template above is "a," and the names of the two cells directly under are "b" and "c." We can derive a complete board from a template by replacing all cell names with cell values. The right board above can be derived by setting the value all cells named a to 4, b to 1, and so on. The replacement must be consistent: every cell with a given name must receive the same value, and no two names can receive the same value.

Board templates can be used to generate multiple boards by replacing the names with different values. The simplest replacement is to set a to 1, b to 2, c to 3, and so on, but there are numerous ways to make replacements. In general, we can generate $n!$ boards of size n by replacing each name with values $0, 1, \ldots, (n-1)$ such that no two names correspond to the same value. On a board of size 10, for example, a single board template can generate $10! = 3,628,800$ boards.

Template Expressions

We don't need to draw out an entire Hexakai board to denote a board template. We can also express a template via more concise template expression, a single string of letters that chains the cell names together. The template expression for the board template above is abcdebcadeebcaddebcadecab. To derive a template expression from a template, take the name of the top cell, make that the first character in the expression, then proceed to the first cell in the next row, append, move to the right and append each character until you're at the end of the row, move to the next row, and so on. At the end of the process, you'll have a template expression of size $n^2$, one character per cell on the board. Both types of expressions - board templates and template expressions - represent the same underlying structure of the board.

Technically, multiple templates, whether they are board templates or template expressions, can describe the same underlying board structure. For example, the template string for the diagram above, abcdebcadeebcaddebcadecab, and the template string edcbadcebaadcebbadcebaced, both refer to the same underlying structure and therefore can be used to generate the exact same boards. All template expressions that map back to the same structure form a group. In other words, a group of expressions consists of all expressions which refer to the same underlying structure. To avoid the hassle of dealing with template expressions that might correspond to the same structures, we can introduce a canonicalization process that will ensure we're only dealing with one unique template expression per board structure.

Canonical Template Expressions

A canonical template expression is a template expression whose variables are denoted in a form of alphabetical order. To define this more precisely, we need to discuss some of the properties of template structures. Each cell name will appear exactly n times in the template expression. Naturally, there must be a position in the template string where it first appears. The first three names always appear in the first, second, and third positions. Subsequent variables are not bound to appear in the fourth, fifth, or higher positions, but they typically first occur relatively close to the beginning of the string. For a given name X, we will denote X's first occurrence in the template expression e as $f(e, X)$. As a utillity, we'll define the ith letter of the alphabet at $alph[i]$, so we can refer to letters by index in the equation below (i.e., $alph[6]$ is the sixth letter of the alphabet, f).

A template expression e is in canonical form (i.e., it's a canonical template expression) if, for each ith name X (ordered by first occurrence, $f(e, X)$), $X = alph[i]$. That is, the first time each ith variable appears, its name must be the ith letter.

The following procedure derives the canonical template expression from any given expression:

  1. Create an empty mapping between names you find in the template expression and the order in which you found them.
  2. For each index in the template expression:
    1. If your mapping doesn't contain the name at that index (i.e., you haven't encountered it yet), add it to the mapping, setting its value as the letter that comes directly after the last unique letter you've found.
    2. In the original template, replace the name at this index with the name that exists in your mapping.

When this procedure is complete, the resulting expression will be a canonical template expression. We'll call this procedure the template canonicalization procedure.

Template Indexing

Template indexing is the process of accessing cell names at specific indices within a template expression. This section outlines how to map back and forth between template expression indices and their corresponding rows and columns on the board. I’ve included this for completeness, as the later sections in this chapter use row and column values instead of index values for calculations. Note that we'll be referring to columns instead of diagonals in many cases for simplicity. A column refers to the index position within a row, so in this sense, columns correspond to diagonals running down-left. Also note, I'll be using the variable i to refer to index, not to the imaginary number.

Mapping Rows and Columns to Indices

In a template expression, each ith character can be mapped to a corresponding row r and column c, and vice verse. We'll start with the latter - the function below, called index, takes a row, column, and gamesize as parameters and calculates the index that cell will belong to in a template expression.

$$ \text{index}(r, c, n) = \left\{ \begin{array}{l@{\;}l} \displaystyle \frac{r(r - 1)}{2} + c & \text{if } r \leq n, \\[12pt] \displaystyle n^2 - \frac{(2n - r + 1)(2n - r)}{2} + c & \text{if } r > n. \end{array} \right. $$

This function is split into two cases: the first case covers the middle row and up, and the second case covers all rows below the middle row. In the first case, it's a matter of calculating how many rows appear above the current row of the index, plus the column position in the current row. The second case is more complex. The term $n^2$ calculates the total size of the board. The term $\frac{(2n - r + 1)(2n - r)}{2}$ calculates the number of cells that encompass row r and all rows below it. These together give us the number of cells above the current row. The last term $c$ adds the column index to the previous sum, yielding the final index.

Mapping Indices to Rows

We can also move in the opposite direction - mapping indices to their corresponding rows and columns, though these computations are more complex. I've separated this into two functions, one that maps the index to the row in which it belongs, and the other to the column in which it belongs.

$$ \text{row}(i, n) = \begin{cases} \left\lceil \frac{\sqrt{8i + 1}}{2} - \frac{1}{2} \right\rceil & \text{if } i \leq \frac{n(n+1)}{2}, \\ 2n - \left\lceil \frac{\sqrt{8(n^2 - i + 1) + 1}}{2} - \frac{1}{2} \right\rceil & \text{if } i > \frac{n(n+1)}{2}. \end{cases} $$

This function takes a similar approach to the previous by handling the two halves of the board separately. The last index of the top half of the board is $\frac{n(n+1)}{2}$ which is the nth triangle number and corresponds to the number of cells on the top half of the board, including the middle row. In the first case, the term $\frac{\sqrt{8i + 1}}{2} - \frac{1}{2}$ is the inverse of the triangle number formula and is based on the quadratic formula. This essentially maps the nth triangle number back to n. The $\lceil \rceil$ braces indicate that we should round up the answer to the next highest whole number. This is necessary because, if the previous term doesn't receive a triangle number, but a number that exists between two triangle numbers (corresponding to a cell in the beginning or middle of a row), we need to round up to correctly specify its row as the next available one. In the second case, the term $n^2 - i + 1$ does two things: it flips the bottom of the board upside down and maps the index to a position on that flipped board. Indices closer to the middle row will map to higher values, and indices closer to the bottom of the board will map to lower values. The surrounding term $\left\lceil \frac{\sqrt{8(n^2 - i + 1) + 1}}{2} - \frac{1}{2} \right\rceil$ operates the same as the quadratic formula in the previous case, essentially swapping i with the previous term, and we subtract this from $2n$ to map the resulting row back to the original Hexakai board.

Mapping Indices to Columns

The column function works with the same fundamental concepts but is notably more complex. To help simplify the expression, we'll reuse the row function above.

$$ \text{col}(i, n) = \begin{cases} i - \frac{(\text{row}(i, n) - 1) \cdot \text{row}(i, n)}{2} & \text{if } i \leq \frac{n(n+1)}{2}, \\[10pt] \text{row}(n^2 - i + 1, n) - \left(n^2 - i + 1 - \frac{(\text{row}(n^2 - i + 1, n) - 1) \cdot \text{row}(n^2 - i + 1, n)}{2}\right) + 1 &\text{if } i > \frac{n(n+1)}{2}. \end{cases} $$

This is conceptually very similar to the previous functions. We split it into two cases again, one for the bottom portion of the board and one for the bottom. In the first case, the term $\frac{(\text{row}(i, n) - 1) \cdot \text{row}(i, n)}{2}$ corresponds to the number of cells in the rows above the row in which the index resides. We subtract this from i, and what remains is the column. In the second case, we flip the board upside down again and map the index to that flipped board, almost exactly the same as what we did in the previous function. The term $n^2 - i + 1$ corresponds to the index of the cell on that flipped board (just like the last function). The term $row(n^2 - i + 1)$ gets the corresponding row on that flipped board, but we're using it here to indicate how many cells belong to that particular row, since the row number and count of cells in that row are equivalent to the top half of a board (or a flipped bottom half with flipped values). The term $\frac{(\text{row}(n^2 - i + 1, n) - 1) \cdot \text{row}(n^2 - i + 1, n)}{2}$ counts the number of cells above the row as derived from the previous term. This is subtracted from $n^2 - i + 1$ to get the column position in that flipped board, and that result is subtracted from $row(n^2 - i + 1)$ to map it back to the column on the original board. However, we need a +1 to map it back properly, because columns are counted from 1: the first cell of a row on the flipped board is the last cell of that row on the original board, not one past it.

Template Transformations

A template transformation is a procedure for deriving a new template from an existing by applying a set of predefined modifications. All hexakai boards can be transformed by reflecting them horizontally and vertically, and some can also be transformed by reflecting them diagonally, or by rotating them. Each of these transformations are defined below.

Horizontal Reflection

For each cell at row r and column c, use the colH function below to calculate $y=colH(r,c,n)$ and swap cell names at r,c with r,y, ignoring cells that have already been swapped. Once complete, apply the template expression canonicalization procedure to derive its canonical form.

$$ \text{colH}(r, c, n) = \begin{cases} r - c + 1 & \text{if } r \leq n, \\ 2n - r - c + 1 & \text{if } r > n. \end{cases} $$

This transformation is valid because the contents of each row remain invariant. Even though the diagonals are reversed in direction, the contents of each diagonal also remain invariant. Therefore, each row and diagonal will continue to contain no duplicates, and each diagonal, plus the center row, will still have exactly one of each value.

Vertical Reflection

For each cell at r,c, use the rowV function below to calculate $x=rowV(r,c,n)$ and swap cell names at r,c with x,c, ignoring cells that have already been swapped. Once complete, apply the template expression canonicalization procedure to derive its canonical form.

$$ \text{rowV}(r, c, n) = 2n - r $$

This transformation is valid because the contents of each row remain invariant. Even though the diagonals are reversed in direction, the contents of each diagonal also remain invariant. Therefore, each row and diagonal will continue to contain no duplicates, and each diagonal, plus the center row, will have one of each value.

180-Degree Rotation

We can yield a third transformation, a 180-degree rotation, by applying both reflections, one after another, in either order.

By applying the two transformations separately, then together, we can yield three more valid templates for a total of four. These four templates can be used to create a total of $4\cdot10! = 14,515,200$ separate boards.

Diagonal Reflection

Unlike the other transformations, diagonal reflection can only be applied to a subset of templates. If a template conforms to the additional constraint that each vertical columns - cells that are positioned above and below each other in the same horizontal position and are not touching - also has no repeated values, it can be subject to the diagonal reflection transformation. Otherwise, it cannot, because applying the transformation would violate the game's rules that no diagonals or rows have repeated values. This board visualizes the vertical columns - each cell shares the same value as other cells in its vertical column. Cells marked with 4 and 5 are the only cells in their own vertical columns.

The procedure is, for each cell at r,c, calculate $x=rowD(r,c,n)$ and $y=colD(r,c,n)$ and swap names at r,c with x,y, ignoring cells that have already been swapped, then canonicalize the template.

$$ \text{rowD}(r, c, n) = 2(c - 1) + 1 + |n - r| $$

$$ \text{colD}(r, c, n) = \begin{cases} c & \text{if } r \leq n \text{ and } c \leq \lfloor r / 2 \rfloor \\ r - c + 1 & \text{if } r \leq n \text{ and } c > \lfloor r / 2 \rfloor \\ c + r - n & \text{if } r > n \text{ and } c \leq \lfloor (2n - r) / 2 \rfloor \\ n - c + 1 & \text{if } r > n \text{ and } c > \lfloor (2n - r) / 2 \rfloor \end{cases} $$

This is valid, assuming the additional constraint previously discussed is satisfied, for the following reasons. Every diagonal in the down-right direction and the down-left direction remain intact, but their positions within the diagonal are mirrored. As a result, every vertical column in the old template becomes a row in the new template, and vice versa.

If a diagonal reflection can be applied to a template, then 90-degree rotations can also be applied by combining diagonal reflection with vertical and/or horizontal reflection. These can also be used to derive diagonal reflections along other axes within the board, yielding a total of eight templates .

The column-uniqueness constraint is much harder to satisfy than the ordinary rules of the game, and the sizes that allow it follow no obvious pattern. An exhaustive computer search shows that no template of size 3, 8, 9, or 10 adheres to it, while templates of size 5, 7, 11, and 12 do. Size 12 is the smallest even size with such a template. The board below is one example: no value repeats in any of its rows, diagonals, or vertical columns, so it can be diagonally reflected and rotated by 90-degrees.

Even sizes are not ruled out, then, but sizes 8 and 10 are, and I haven't searched beyond size 12. I haven't yet studied this phenomenon specifically, but I suspect it has some relation to my observation that my algorithm for generating Hexakai boards has much more trouble on even sized boards than it does on odd sized boards.

Cell Constraints and Their Relationships

Every cell constrains, and is constrained by all cells that lie within its row and two intersecting diagonals. This means that, for any given cell c, if c is assigned a value v, no other cell that c constrains can also be assigned v. Similarly, if any cell that constrains c has a value v, c itself cannot receive that value.

Single Cell Constraints

For a game of size n, each cell is constrained by the $2 \cdot (n - 1)$ cells in its two diagonals, and by the other cells in its row, which varies from $0$ to $n - 1$ depending upon the size of its row. The topmost cell and bottommost cell don't have any row constraints, so they only constrain $2 \cdot (n - 1)$ cells. Cells in each subsequent row of size r constrain an additional $r - 1$ cells. The middle row of any board is larger than all other rows with its n cells, so each cell in that row constrains $3 \cdot (n - 1)$ cells. The closer a cell is to the middle row, the more cells it constrains, and the farther away, the fewer.

All cells constrain the same number of cells they are constrained by. If a cell x constrains cell y, then cell y also constrains cell x. For any given cell at row r and column c, the number of cells it constrains can be calculated by the function below. Note that the function doesn't include a parameter for the column because the column position doesn't affect how many cells any given cell constrains.

$$ \text{constrains}(r, n) = \begin{cases} 2(n - 1) + (r - 1) & \text{if } r \leq n, \\ 2(n - 1) + (2n - r - 1) & \text{otherwise.} \end{cases} $$

This concept can be used directly when solving boards of larger sizes and higher difficulties. If you need to apply a tentative assignment strategy, many of which are discussed on my post on Hexakai strategies, finding the cells that constrains the higher number of other cells and focusing your tentative assignments on those will almost certainly significantly reduce the amount of time you spend on the wrong tentative assignment.

Sum of Constraints

We can determine the total number of constraints on the board by summing the number of constraints on each cell. Each cell in the ith row constrains the same amount of cells as the other cells in its row, so we can re-use the formula above. As is typical with mathematics involving Hexakai boards, the formula above splits between two cases, one for the top half of the board, including the middle row, and one for the bottom. We can handle these cases separately with two summations:

$$ \text{num_constraints}(n) = \sum_{r=1}^{n} r \left[ 2(n - 1) + (r - 1) \right] + \sum_{r=n+1}^{2n - 1} (2n - r) \left[ 2(n - 1) + (2n - r - 1) \right] $$

In Hexakai, if a cell constrains another cell, it is also constrained by that cell. In other words, if cell A constrains cell B, then cell B necessarily constrains cell A. Therefore, we can treat a constraint as a bidirectional relationship. With this in mind, the function above double-counts constraints. To rectify this, we'll revisit the function and break it down into two parts: constraints to cells within the same row, and constraints to cells within different rows. In the former, for each cell, we only need to count constraints to cells to its right, since the constraints to its left will have already been counted. In the latter, we only need to count constraints to cells lower on the board. We'll call these forward constraints, since we're only considering constraints farther along in the board.

In this board, the value in each cell represents the number of cells it constrains within its row. The topmost row doesn't constrain any other cells in its row, simply because there are none. In the next row, the leftmost cell constrains one other cell, followed by 0, then the next row constrains 2, 1, and 0, and so on. When the bottom half of the board is reached, the pattern repeats in reverse.

Each row essentially counts-down the number of cells. If we sum the values of each row individually, we'll form triangle numbers. We can model the total sum of row-constraints across the board using this summation, where the triangle symbol indicates the triangle function (which we've covered in sections above).

$$ \text{forward_constrains_row}(n) = \sum_{r=1}^{n} \triangle(r - 1) + \sum_{r=n+1}^{2n - 1} \triangle(2n - 1 - r) $$

In this board, the value in each cell represents the number of cells it constrains within its intersecting diagonals, only including cells below it. A few interesting patterns emerge. Most notably, there is an apparent countdown from cells in the top row to cells in the bottom row. This is analogous to the previous function, where the countdown appears within rows.

To derive this pattern, notice that the topmost cell constrains exactly eight cells, four in each diagonal direction. In the row below, each cell constrains seven. The first constrains three in its left diagonal and four in its right diagonal, and the second contains the opposite. In general, each cell in each row may constrain different counts on its left and right sides, but the total counts per cell remains invariant within the row. As usual, the logic holds for the top half, then flips and holds on the lower half. We can model the total sum of diagonal-constraints across the board using this summation.

$$ \text{forward_constrains_diagonal}(n) = \sum_{i=1}^{n} i(2n - 1 - i) + \sum_{i=1}^{n - 1} i(i - 1) $$

Combining the two, we can derive the total number of constraints on the board, simplified to the cubic below:

$$ \text{forward_constrains}(n) = \text{forward_constrains_row}(n) + \text{forward_constrains_diagonal}(n) = \frac{4}{3}n^3 - \frac{3}{2}n^2 + \frac{1}{6}n $$

The table below compares the values of this function for each game size and its corresponding numbe of cells.

Game Size ($n$) Number of Cells ($n^2$) Number of Constraints ($\frac{4}{3}n^3 - \frac{3}{2}n^2 + \frac{1}{6}n$)
110
245
3923
41662
525130
636235
749385
864588
981852
101001185
111211595
121442090
131692678
141963367
152254165
162565080

Information Clustering and Distribution

This section won't involve a formulaic analysis, but it is worth discussing the general ideas behind information clustering and distribution pertaining to puzzle boards. In this context, information refers to non-empty cells, information clustering refers to the degree at which non-empty cells are clustered, and information distribution refers to the degree at which non-empty cells are distributed throughout the entire board. Consider the two puzzle boards below:

The first board was generated using the Standard generator pattern, available under "Advanced Options" on the main app. The second board was generated using the Top Heavy generator pattern. The first board has a much higher degree of information distribution, whereas the second board has a much higher degree of information clustering at the top and empty-cell clustering, or missing-information clustering at the bottom. The second board is both harder to generate and harder to solve because of its lack of information distribution.

Imagine playing through the second puzzle yourself. A natural strategy would be to solve the topmost cell, which must have a value of , then proceed with making assignments to the leftmost and rightmost cells, then work toward the middle. However, the ambiguity suddenly and significantly increases as you apply this strategy to the point where you'll almost immediately need to use advanced strategies, including advanced forms of tentative assignment. This is essentially because all of the information provided by the original, hard-coded cells is clustered within the portion of the board that's already solved, thereby reducing their constraining power on the empty cells.

We can measure the level of complexity, uncertainty, and entropy using various approaches. My app uses a measure of uncertainty, which can also stand as a measure of complexity or as an estimation of entropy, to generate boards according to the difficulty level the user has selected. There are numerous ways we can approach such a measurement. A simple starting point would be to count the number of possible values each empty cell can be assigned at face-value (without applying strategies to dive deeper into the board), add them together, and divide them by the size of the board, something like the equation below, where i is the index of the cell and num_values counts the number of values that can be potentially assigned to it.

$$ \text{uncertainty} = \frac{1}{n^2} \sum_{i=1}^{n^2} \text{num_values}(i) $$

In this board, we have a total of 25 cells, 14 of which are empty. In the first empty cell, we can potentially assign 3 values: , , or . The next cell has only one potential value, , so the running sum so far is 4. The following empty cell can have two values, bringing the running sum up to 6. The total sum over the entire board is 24. Then, by dividing against the size of the board, we get the final result of $\frac{24}{25} = 0.96$. This gives us a potentially useful measure, but what exactly does 0.96 mean? What are the lowest and highest values we can get from this measure? Do those make sense, and how can we improve this?

First, this measure will be more useful if we can confine it to a range between 0 and 1, where 0 indicates the simplest possible board, a complete board, and 1 represents a totally ambiguous board, possibly a board with no filled cells. We can normalize the equation above to that range by dividing the entire function by n:

$$ \text{uncertainty} = \frac{1}{n^3} \sum_{i=1}^{n^2} \text{num_values}(i) $$

This is more useful, as it gives us a normalized range of output, and the value tends to increase as the number of empty cells, and even the percieved difficulty of the board, increase. I use a more sophisticated version of this function that considers cell constraints and other board-specific properties in my board generation algorithm to help determine the level of uncertainty of the generated boards to ensure they match the requested difficulty of the game to be generated.

Calculating the Number of Boards

Complete Boards

We will start by defining an upper bounds for the total number of complete boards for a game of size n. Each game has $n^2$ cells, and each cell has a range of $n$ values which we can assign it, so our upper bound is $n^{n^2}$. This figure is significantly higher than the actual number and represents the number of total boards that can be derived by assigning all permutations of all values on the board, ignoring Hexakai's usual constraints.

To improve, let's consider the number of boards that have each of n possible values exactly n times. We can calculate this by determining the number of ways we can select n individual cells from a board with $n^2$ cells, denoted as ${n^2\choose n}$, then multiply this by the number of ways we can select n more cells from the remaining $n^2 - n$ cells, and repeat until all cells are selected. The first group of cells we select receives the first value, the second group receives the second value, and so on, so every sequence of selections is exactly one board. Note that we don't multiply by $n!$ here, as we do when deriving boards from a template: the groups are already selected in order, and that order is what decides which value each group receives. Dividing this function by $n!$ would instead give the number of templates, since a template doesn't care which group is which. The total function is:

$$ \text{count}(n) = \prod_{k=0}^{n-1} \binom{n^2 - k \cdot n}{n} $$

This provides a significant increase in precision by substantially lowering the upper bound. For example, when $n=10$, the refined upper bound is more than 40 million times smaller than the previous upper bound. However, this bound is still much higher than the true value. When we select n cells from the remaining $n^2 - k \cdot n$ during each iteration (as well as from the first $n^2$ cells), we're not filtering for selections that have more than one value on the same row or diagonal. In other words, we're not considering the constraints of each cell in this calculation. To remedy this, I'll include a term that lightly considers constraints using the lower bount of $2 \cdot (n - 1)$, which is the number of cells the top and bottom cells each constrain. I'm picking those to ensure that we use the lower bound of constrain to ensure we don't over aggresively filter out possible selections. I'm going to subtract cells in each product iteration to ensure we don't filter out cells that were already selected: $2 \cdot (n - 1) - k \cdot n$, and finally, I'll use a $max(0, ...)$ to ensure that, once we reach a few iterations, we're not filtering anymore, favoring inclusion over exclusion. The full function is:

$$ \text{count}(n) = \prod_{k=0}^{n-1} \binom{n^2 - k \cdot n - \max(0, 2(n-1) - k \cdot n)}{n} $$

This gives us another substantial tightening of the upper bounds, but it is still very high. For smaller values of n, the estimates are ridiculously large compared to the actual values: $100$ vs $6$ for $n=3$ and $28,977,204,320,064$ vs 360 for $n=5$. I haven't calculated the true values for $n>5$, but I suspect this estimation function actually becomes more accurate as n becomes larger, mainly because the number of possible templates grows astronomically for each increase in n.

This function estimates the number of complete boards for each game size to be:

n Count
3 100
5 2.90 × 1013
7 3.37 × 1035
8 8.33 × 1050
9 2.45 × 1069
10 1.09 × 1091
11 9.16 × 10115
12 1.77 × 10144
13 9.43 × 10175
14 1.63 × 10211
15 1.06 × 10250
16 3.00 × 10292

If I were to attempt to refine this further, I would need to design an algorithm that searches through possible valid groupings, finds and filters non-visited constraining cells, and calculates the exact number of cells to filter at each iteration of the product, or more likely, transform this entirely into a graph-based search, something far too complex to represent as a single, simple mathematical function.

Puzzle Boards

We can think of puzzle boards as versions of complete boards with some or many cells removed, so long as the puzzle has exactly one solution - you shouldn't be able to fill in cells in more than one way that leads to a valid board, and it must be possible to find a solution. There are exponentially more puzzle boards than complete boards. I've not mathematically calculated the number of puzzle boards that can exist, but my algorithm for generating Hexakai boards can only remove up to roughly 80% of the cells on a board while maintaining the constraints above, so I suspect the theoretical limit lies somewhat near this value and will use it in our analysis here.

Consider a process that starts with a complete board and systematically removes cell values. For any given board, the number of cells we can remove, which we'll refer to as q, can range from 1 to $0.80 \cdot n^2$. For each value of q, there are at most ${n^2\choose q}$ ways of selecting q cells from a complete board to make it a puzzle board. This is an upper bound estimate, since some selections will result in invalid puzzle boards, a number that will grow as q becomes larger.

For a single complete board, the upper bound estimate for the number of puzzle boards it can yield is given in the function below. To determine the upper bound of the number of boards that can be generated from a template, multiply the result of the function by $n!$.

$$ \text{puzzles}(n) = \sum_{q=1}^{0.80n^2} \binom{n^2}{q} $$

To determine the upper bound for the total number of puzzle boards for each game size n, multiply the two functions together:

$$ \text{nPuzzles}(n) = \text{count}(n) \cdot \text{puzzles}(n) = \left( \prod_{k=0}^{n-1} \binom{n^2 - k \cdot n - \max(0, 2(n-1) - k \cdot n)}{n} \right) \cdot \left( \sum_{q=1}^{0.80n^2} \binom{n^2}{q} \right) $$

This function estimates the number of puzzle boards for each game size to be the values shown in the table below. These values are certainly large overestimations, as they're composed of two generous upper bound estimations whose errors multipy together, especially for smaller values of n:

n Count
35.01 × 104
59.72 × 1020
71.90 × 1050
81.54 × 1070
95.91 × 1093
101.38 × 10121
112.44 × 10152
123.95 × 10187
137.06 × 10226
141.63 × 10270
155.72 × 10317
163.48 × 10369

Algorithmic Complexity

The process of generating valid Hexakai boards, both complete and puzzle, has significantly higher complexity than the mathematical functions we've discussed so far, especially when considerations of space and time efficiency are raised. When software engineers and computer scientists analyze problems that algorithms can solve, such as the problem of generating a valid Hexakai board, we measure the algorithm's complexity (essentially a measure of efficiency) based on how much space it needs and how many computational steps it requires.

There are two ways we can measure these. We can execute an algorithm repeatedly with different inputs and measure how much space they consume in terms of bytes and how much time they consume in terms of some unit of time, such as milliseconds, or we can analyze the algorithm from a theoretical perspective and determine how much space and time they consume with respect to the input size based on the nature of the algorithm itself. The former is important when creating software that is intended to be used by customers to ensure that actual performance falls within an acceptable threshold. The latter is important when engineers and scientists want to assess the core nature of the problem that they’re trying to solve, learn how large the problem becomes when its inputs grow, and if their problem is closely related to any other problems. For example, how closely is generating a board of Hexakai related to the biological process of protein folding, or finding the best move to make in a game of chess, or to generating a Sudoku board? In this section, we’ll focus our analysis on the theoretical aspect.

The input size for the algorithm that generates Hexakai boards is n, the size of the board to be generated. Our analysis of the space and time consumed by the process of generating a Hexakai board will be written in terms of n. We don’t need to find the exact function or formula that describes exactly how much space and time would be consumed, we’re only interested in a "big-picture" approximation, or more precisely, an asymptotic estimate.

We could find this approximation by diving deep into my board generation algorithm, analyzing each step with excruciating detail and assessing how they all fit together. That would be highly tedious, time consuming, and error prone. Instead, we’ll take the equally valid approach of relating this problem to an existing problem whose complexity has already been determined. This is done through a process called problem reduction: converting one problem into another, so that what is known about one tells us something about the other.

First, though, it helps to have a ceiling. The bluntest way to fill a Hexakai board is brute force: try every value in every cell and keep the arrangements that follow the rules. There are n values to choose from and $n^2$ cells on the board, so brute force examines up to $n^{n^2}$ arrangements. This monstrous super-exponential figure matches the original upper bound estimate of $n^{n^2}$ boards for a given size n.

Before continuing, note that while this estimate of $n^{n^2}$ would be prohibitively high on its own, there are numerous strategies that we can, and I do, employ to significantly reduce that complexity in a practical sense, even if the theoretic upper bound remains intact. Strategies like search space elimination, search heuristics, and precomputation all serve to make board generation possible despite this high upper bound, even as n approaches larger values like 16.

Computer scientists sort problems into classes according to how hard they are, and one of the most interesting classes is called NP-complete. A problem is NP-complete when it meets two conditions: a proposed solution can be checked quickly (the problem is in NP), and it is at least as hard as every other problem in NP (the problem is NP-hard). The natural Hexakai version of the question is the one a player faces: given a puzzle board, can its empty cells be filled in to make a valid complete board?

That problem is in NP. To check a filled-in board, we only need to confirm that no two cells that constrain each other share a value, and we've already seen that the number of constraints on a board is a cubic function of n.

The second condition is where problem reduction comes in. We need to show that a problem already known to be NP-complete can be converted into a Hexakai puzzle. There is a natural candidate. A Hexakai board is essentially a Latin square, a grid in which every row and column contains each value exactly once, where Hexakai's two diagonal directions play the roles of the square's rows and columns. Hexakai then adds the constraint that its rows, which run along one diagonal direction of that square, also contain no repeated values. Completing a partially filled Latin square was proven NP-complete by Charles Colbourn in 1984, and Sudoku was proven NP-complete in 2003 by Takayuki Yato and Takahiro Seta, who embedded a Latin square puzzle inside a larger, mostly prefilled Sudoku puzzle. Colbourn's result doesn't carry over to Hexakai automatically, because the additional constraint makes it a different problem. The same idea of embedding does work, though: any Latin square puzzle can be converted into a mostly prefilled Hexakai puzzle that has exactly the same solutions. Hexakai is therefore NP-complete, as non-trivial single player games that involve assigning values and eliminating possibilities typically are. The full proof is more formal than the rest of this post, so I've given it its own section at the end, just before the conclusion.

It is possible to write efficient algorithms to solve problems in this category, but it requires sophisticated implementations and significant search space reductions - the elimination of large groups of invalid assignments - to be feasible for large, or medium input sizes, in a practical sense.

There is an interesting open problem in computer science called P versus NP which considers how different classes of algorithms relate to each other. This problem was first introduced in 1971 and is still unsolved to this day. The Clay Mathematics Institute established seven Prize Problems in May of 2000, one of which is the P versus NP problem, and will award one million dollars to anyone who can solve it. It would be an interesting turn of events, surreal, if Hexakai were to somehow be involved in the solution to P versus NP.

Proof: Hexakai Completion is NP-Complete

This section proves the claim made above: deciding whether a partially filled Hexakai board can be completed is NP-complete. It is more formal than the rest of this post, and you can skip to the conclusion without losing anything else. The proof converts any Latin square puzzle into a Hexakai puzzle with exactly the same solutions. The idea of embedding a Latin square puzzle in a larger, mostly prefilled grid is inspired by the projection argument Yato and Seta used to prove that Sudoku is NP-complete.

Throughout, the proof views a Hexakai board as if it were a Latin square with one additional constraint that the diagonal direction must also have no duplicate values. Turn the board so that its two diagonal directions become the rows and columns of a square grid, each of which must contain every value exactly once, just as in a Latin square. The game's horizontal rows then become the grid's anti-diagonals, the lines that run from the bottom-left to the top-right, and the additional constraint is that no anti-diagonal may contain the same value twice.

Definitions

The proof relies on the following terms and notation.

One note on variables. Everywhere else in this post, n is the size of a Hexakai board. In this section only, $N$ is the size of the Hexakai board, $n$ is the order of a Latin puzzle, and $m$ is an odd number derived from $n$.

The Problem and the Result

Hexakai Completion. This is the problem of deciding whether a Hexakai puzzle can be solved. We are given a Hexakai puzzle of size $N$, drawn as a grid with $N$ rows and $N$ columns in which each cell is either empty or holds one of $N$ values. We are asked whether the empty cells can be filled so that no value repeats in any row, any column, or any anti-diagonal.

Rows and columns have $N$ cells, so each must contain every value exactly once. Anti-diagonals have between $1$ and $N$ cells, so they only forbid repeats.

This grid is the Hexakai board redrawn, turned so that its two diagonal directions run straight across and straight down. The table below shows how the two correspond. For the rest of this section, "row" and "column" refer to the rows and columns of this grid, not to the horizontal rows of the game board.

Game boardGridRule
Diagonals in the first directionRowsEvery value exactly once
Diagonals in the second directionColumnsEvery value exactly once
Horizontal rows, $1$ to $N$ cells longAnti-diagonalsNo repeated values

Latin Square Completion. This is the problem of deciding whether a Latin puzzle can be solved. We are given a Latin puzzle of order $n$, and we are asked whether it has a completion. Colbourn proved this problem NP-complete in 1984.

Theorem. Hexakai Completion is NP-complete.

Proof Outline

The proof shows membership in NP, then converts any Latin puzzle into a Hexakai puzzle with exactly the same completions, in four steps:

  1. Pad. A Latin puzzle of order $n$ becomes a Latin puzzle of odd order $m = 2n + 1$.
  2. Build a Hexakai base board. A formula gives a complete Hexakai board of size $N = m^3$.
  3. Find a Latin subsquare. The base board contains $m^2$ cells, where $m$ of its rows cross $m$ of its columns, that form a Latin square on $m$ of its symbols and that the anti-diagonal rule never constrains.
  4. Fill the subsquare. Erase the Latin subsquare and write the padded Latin puzzle in its place. The Hexakai puzzle can be completed exactly when the Latin puzzle can.

Membership in NP

A completed grid is a certificate of $N^2$ values. Checking it means confirming that it agrees with the given clues and that none of the $N$ rows, $N$ columns and $2N - 1$ anti-diagonals repeats a value, which takes time polynomial in $N$.

Step 1: Padding to Odd Order

Every Latin puzzle of order $n$ converts to a Latin puzzle of odd order $m = 2n + 1$ with the same completions. This is needed for even $m$ because the base board in Step 2 only works for odd $m$, so this step acts as as additional preparation.

Construction. Let $P$ be a Latin puzzle of order $n$ on the original symbols $1, \ldots, n$. Add $n + 1$ new symbols $k(0), \ldots, k(n)$, and treat the argument of $k$ modulo $n + 1$, so that $k(n + 1)$ is $k(0)$ and $k(-1)$ is $k(n)$. Index the first $n$ rows by $u$ and the first $n$ columns by $v$, each from $0$ to $n - 1$. Index the $n + 1$ extra rows by $w$ and the $n + 1$ extra columns by $w'$, each from $0$ to $n$. The padded puzzle $P'$ is made of these blocks:

BlockCellContents
Top-leftrow $u$, column $v$The cell of $P$: its clue, or empty
Top-rightrow $u$, extra column $w'$$k(u + w')$
Bottom-leftextra row $w$, column $v$$k(w + v)$
Bottom-right, diagonalextra row $w$, extra column $w$$k(w - 1)$
Bottom-right, off-diagonalextra row $w$, extra column $w' \neq w$The original symbol $(w' - w) \bmod (n + 1)$

For $n = 2$, writing the new symbols as a, b and c, the padded puzzle is:

abc
bca
abc12
bc2a1
ca12b

The shaded cells are the empty cells of $P$. The heavy lines separate the four blocks.

Lemma 1. The completions of $P'$ are exactly the completions of $P$ placed in the top-left block.

Proof. Every completion of $P'$ contains a completion of $P$. Row $u$ already holds $k(u), k(u + 1), \ldots, k(u + n)$ in its top-right cells, which is every new symbol. Its $n$ top-left cells must therefore hold the $n$ original symbols, once each. Columns forbid repeats, so the top-left block is a Latin square on $1, \ldots, n$ that agrees with the clues of $P$.

Every completion $L$ of $P$ extends to a completion of $P'$. Put $L$ in the top-left block and check each line:

From here on, $m$ is odd and at least 3, and the Latin puzzle has order $m$.

Step 2: The Base Board

For every odd $m$ there is an explicit complete Hexakai board of size $N = m^3$.

That some board of this size exists is already guaranteed. When $m$ is odd, $m^3$ is odd too, and we showed earlier in this post that a complete board exists for every odd size. The reduction needs more than existence, though: it needs a board that also contains the Latin subsquare found in Step 3, so this step builds a particular one. Its formula uses the same idea as the earlier construction, a shift of 2 from one row to the next, applied to each base $m$ digit.

Notation. Number the rows and the columns from $0$ to $N - 1$, and write each row index $i$ and column index $j$ in base $m$. The digits $a, b, c, p, q, r$ each lie between $0$ and $m - 1$:

$$ i = a + bm + cm^2, \qquad j = p + qm + rm^2 $$

The $N$ symbols are the triples $(x, y, z)$ of digits, and $x$, $y$ and $z$ are the first, second and third parts of the symbol. Written as a single number, the triple is $x + ym + zm^2$.

Definition. The base board $B$ puts this symbol in row $i$, column $j$, with each part taken modulo $m$:

$$ B(i,j) = \bigl(2a + p,\;\; 2a + 2b + q,\;\; b + 2c + r\bigr) \bmod m $$

Lemma 2 (rows). Each row of $B$ contains every symbol exactly once.

Proof. Fix $i$, so $a$, $b$ and $c$ are constants. The map from $(p, q, r)$ to the symbol adds a constant to each digit modulo $m$, which is a bijection on triples.

Lemma 3 (columns). For odd $m$, each column of $B$ contains every symbol exactly once.

Proof. Fix $j$, so $p$, $q$ and $r$ are constants. Since $m$ is odd, we can divide by 2 modulo $m$, and the row digits can be recovered from the symbol $(x, y, z)$ in turn: $a = (x - p)/2$, then $b = (y - q - 2a)/2$, then $c = (z - r - b)/2$, all modulo $m$. Different rows therefore give different symbols, and a column has as many cells as there are symbols.

Note: why Step 1 was needed. This proof is the reason for padding the Latin puzzle to an odd order, and it is the one place where an even $m$ would break the base board. When $m$ is even, we cannot divide by 2 modulo $m$, because doubling no longer gives every remainder: $2a$ and $2\left(a + \frac{m}{2}\right) = 2a + m$ leave the same remainder. The row digit $a$ only ever enters the formula for $B$ as $2a$, so two rows whose first digits are $a$ and $a + \frac{m}{2}$, and whose other digits match, would hold exactly the same symbols in every column. With $m = 2$, for example, the first two rows of the board would be identical, and every column would repeat a symbol. Step 1 guarantees this never happens, because $m = 2n + 1$ is odd for every $n$.

Lemma 4 (anti-diagonals). No anti-diagonal of $B$ contains a symbol twice.

Proof. Suppose cells $(i, j)$ and $(i', j')$ satisfy $i + j = i' + j'$ and hold the same symbol. Write the digits of the second cell with primes.

  1. Reducing $i + j = i' + j'$ modulo $m$ gives $a + p \equiv a' + p'$. The first parts of the symbols give $2a + p \equiv 2a' + p'$. Subtracting, $a \equiv a'$, so $a = a'$ and then $p = p'$.
  2. Remove $a + p$ from both sides of $i + j = i' + j'$ and divide by $m$, which leaves $(b + q) + (c + r)m = (b' + q') + (c' + r')m$. Reducing modulo $m$ gives $b + q \equiv b' + q'$. The second parts, with $a = a'$, give $2b + q \equiv 2b' + q'$. Subtracting, $b = b'$ and then $q = q'$.
  3. What remains is $c + r = c' + r'$. The third parts, with $b = b'$, give $2c + r \equiv 2c' + r'$. Subtracting, $c = c'$.

So $i = i'$, and the two cells are the same cell.

By Lemmas 2 to 4, $B$ is a valid complete Hexakai board whenever $m$ is odd. Lemma 4 holds for every $m$; only Lemma 3 needs $m$ to be odd.

Step 3: The Latin Subsquare

A Latin subsquare of a grid is a smaller Latin square sitting inside it. To find one, choose $k$ of the grid’s rows and $k$ of its columns, which do not need to be next to each other, and look only at the $k^2$ cells where a chosen row crosses a chosen column. If those cells use just $k$ different symbols, with each symbol appearing once in every chosen row and once in every chosen column, then together they form a Latin square of order $k$, and we call them a Latin subsquare of order $k$.

The base board contains a Latin subsquare of order $m$ that behaves like a free Latin square: only $m$ symbols can go in its cells, and the anti-diagonal rule never constrains them.

Definitions.

Lemma 5 (the Latin subsquare). The subsquare cell in row $u$, column $v(m + 1)$ holds the special symbol with $d = (2u + v) \bmod m$. Consequently:

Proof. Row $u$ has digits $(u, 0, 0)$. Column $v(m + 1) = v + vm$ has digits $(v, v, 0)$. The formula gives $(2u + v, 2u + v, 0)$. For fixed $u$ the map from $v$ to $2u + v$ is a bijection modulo $m$, and for fixed $v$ the map from $u$ to $2u + v$ is a bijection because $m$ is odd. So each special symbol appears once among the subsquare cells of each subsquare row and subsquare column. By Lemmas 2 and 3 each symbol appears only once in a whole row or column, so the special symbols appear nowhere else in those lines.

Lemma 6 (separate anti-diagonals). No two subsquare cells lie on the same anti-diagonal.

Proof. The subsquare cell at row $u$, column $v(m + 1)$ lies on anti-diagonal $u + v(m + 1)$. Since $u$ is less than $m + 1$, this number determines both $u$ and $v$.

Lemma 7 (isolation). For $m \ge 3$, the only cell on a subsquare cell's anti-diagonal that holds a special symbol is the subsquare cell itself.

Proof. Let the subsquare cell be at row $u$, column $t(m + 1)$, and let $s = u + t(m + 1)$ be its anti-diagonal. Then $s \le (m - 1) + (m - 1)(m + 1) = m^2 + m - 2$.

Take any cell on this anti-diagonal, with row digits $(a, b, c)$ and column digits $(p, q, r)$. Its symbol is special exactly when its first two parts agree and its third part is $0$:

$$ p \equiv 2b + q \pmod m, \qquad b + 2c + r \equiv 0 \pmod m $$

The cell's row index plus its column index equals $s$, and $s \lt 2m^2$, so $c + r \le 1$. Three cases remain:

CaseForced by the second conditionOutcome
$c = 0$, $r = 0$ $b = 0$, and then $p = q$ by the first condition The row is $a$ and the column is $p(m + 1)$, so $a + p(m + 1) = u + t(m + 1)$. Both $a$ and $u$ are less than $m + 1$, so $a = u$ and $p = t$. This is the subsquare cell itself.
$c = 1$, $r = 0$ $b = m - 2$ The row index is at least $m^2 + (m - 2)m = 2m^2 - 2m$. This exceeds $m^2 + m - 2$, because the difference is $(m - 1)(m - 2) \gt 0$. The row index would exceed $s$, which is impossible.
$c = 0$, $r = 1$ $b = m - 1$ The row index plus the column index is at least $(m - 1)m + m^2 = 2m^2 - m$. This exceeds $m^2 + m - 2$, because the difference is $m^2 - 2m + 2 \gt 0$. Impossible.

So the subsquare cell is the only one.

Step 4: The Reduction

Writing a Latin puzzle into the Latin subsquare gives a Hexakai puzzle with exactly the same completions.

Construction. Let $Q$ be a Latin puzzle of odd order $m \ge 3$, with its symbols renamed $0, \ldots, m - 1$. Define the Hexakai puzzle $G$ of size $N = m^3$:

Lemma 8 (equivalence). The completions of $G$ correspond one-to-one with the completions of $Q$.

Proof. From $Q$ to $G$. Let $L$ be a Latin square completing $Q$. Fill each empty subsquare cell with the special symbol named by $L$, and check each line of the result:

From $G$ to $Q$. Take any completion of $G$. In a subsquare row, the filler cells already hold every ordinary symbol (Lemma 5), so the $m$ subsquare cells must hold the $m$ special symbols, once each. The same holds for each subsquare column. Reading the subsquare cells as a grid with $m$ rows and $m$ columns therefore gives a Latin square on the special symbols that agrees with the clues of $Q$.

The two directions undo each other, so the correspondence is one-to-one.

Proof of the theorem. Membership in NP is shown above. For hardness, take any Latin puzzle $P$ of order $n$. Apply Step 1 to get a puzzle of odd order $m = 2n + 1 \ge 3$, then the construction above to get a Hexakai puzzle $G$. By Lemmas 1 and 8, $G$ can be completed exactly when $P$ can. $G$ has $(2n + 1)^6$ cells, and each is computed by a fixed number of arithmetic steps, so the conversion runs in polynomial time. Latin Square Completion is NP-complete, so Hexakai Completion is NP-hard, and since it is also in NP, it is NP-complete.

Remark. The conversion preserves the number of completions, not just whether one exists. Results about counting completions, or about whether a Latin puzzle's completion is unique, therefore carry over to Hexakai as well.

Example of Concept

A Latin puzzle of order 3 becomes a Hexakai puzzle of size 27, in which 720 of the 729 cells are filler cells. Since 3 is already odd, padding is skipped and $m = 3$.

The symbols are written as the numbers 0 to 26. The subsquare cells are rows 0, 1 and 2 crossed with columns 0, 4 and 8, and the special symbols are 0, 4 and 8. This is the top-left corner of the base board, with the subsquare cells highlighted:

col 012345678
row 0012345678…
row 1867201534…
row 2453786120…
⋮⋮⋮⋮⋮⋮⋮⋮⋮

The highlighted cells are the nine subsquare cells. They hold only the special symbols 0, 4 and 8.

Columns 9 to 26 of these rows hold the symbols 9 to 26, and rows 3 to 26 complete the board. The subsquare cells lie on anti-diagonals 0, 4, 8, 1, 5, 9, 2, 6 and 10, which are all different.

All of the Latin puzzle lives in the first three rows, so it is fair to ask why the other 24 rows are needed. There are three reasons:

Because those 24 rows are entirely clues, they add nothing to solve. All of the difficulty of the Hexakai puzzle sits in the nine subsquare cells, which is exactly the difficulty of the Latin puzzle.

Take this Latin puzzle and rename its symbols so that 1 becomes 0, 2 becomes 4, and 3 becomes 8:

Latin puzzle

1
2

In the subsquare

0
4

The shaded cells are empty.

The Hexakai puzzle is the base board with its nine subsquare cells replaced by the right-hand grid: a 0 at row 0, column 0, a 4 at row 1, column 4, and seven empty cells. Each of rows 0 to 2 and columns 0, 4 and 8 is missing only special symbols, so the seven empty cells can only take 0, 4 or 8.

This Latin puzzle has exactly one completion, so the Hexakai puzzle does too:

Latin solution

132
321
213

In the subsquare

084
840
408

Try It Yourself

The tool below generates a random Latin puzzle of order 3 and shows the Hexakai puzzle of size 27 that the reduction builds from it. The Latin puzzle's symbols 1, 2 and 3 are renamed to the special symbols 0, 4 and 8, and its nine cells are written into the subsquare cells, which are outlined on the large board. Every other cell of the board is a filler cell and never changes. Any way of completing the Latin puzzle completes the Hexakai puzzle, and the reverse is also true.

Latin puzzle

In the subsquare

The Hexakai puzzle of size 27, drawn as a grid. The outlined cells are the subsquare cells, and the shaded ones are empty.

Conclusion

The information we've covered greatly aided my efforts in making Hexakai fast in execution and vast in the number of boards it can generate. There are still some open questions I'd like to address if and when I can dedicate time, including: