The Quaternary Gray Code and Ziggu Puzzles
Abstract
We investigate solutions to the new “Ziggu” family of sequential puzzles including Ziggurat, Zigguflat, Zigguhooked and so on. These puzzles have pieces that form mazes. We encode the state of each puzzle as a quaternary number (i.e., base ) with digits, where each digit gives the horizontal or vertical position in one maze. For example, the commercial version of Zigguflat has pieces connected into mazes and its state requires digits to describe. We show that the number of states on a shortest solution is (Oeis A101946). There is only one solution of this length, and it is generated from the start configuration by a simple algorithm: make the leftmost modification that doesn’t undo the previous modification. Replacing “leftmost” with “rightmost” instead generates the unique longest solution that visits all states (Oeis A003462). In this way, Ziggu puzzles can be viewed as -ary, -ary, or -ary puzzles based on how the number of state encodings, valid states, or minimum states grow with each additional maze.
Classic Gray code puzzles (e.g., Spin-Out) provide natural and illuminating comparisons. These puzzles with pieces typically have (Oeis A000079) or (Oeis A000975 [21]) states on their unique (shortest) solution, and at most one modification doesn’t undo the previous modification. The states visited in a Gray code puzzle solution follow the well-known binary reflected Gray code. We show that Ziggu puzzles instead follow the quaternary reflected Gray code. More specifically, the shortest and longest solutions are both sublists of this order, meaning that some quaternary words are skipped over but the relative order of the remaining words does not change.
These results show how to solve Ziggu puzzles from the start configuration. To help solve the puzzle from an arbitrary configuration we provide -time comparison, and successor algorithms, which give the relative order of two states and the next state, respectively. While Gray code puzzles have simpler recursive descriptions and successor rules, a Ziggu puzzle has a much simpler loopless algorithm to generate its shortest solution than the Gray code puzzles do. The two families are also intimately related as they have the same comparison function.
Keywords and phrases:
Puzzle, Ziggu, Ziggurat, Zigguflat, Gray Code, Loopless AlgorithmFunding:
Madeleine Goertz: The first author would like to thank the William and Linda Frost Fund in the Cal Poly College of Science and Mathematics for their generous financial support.Copyright and License:
2012 ACM Subject Classification:
Mathematics of computing Combinatorial algorithms ; Mathematics of computing Combinatorics on wordsRelated Version:
For additional technical results, see the preliminary version of this article.Acknowledgements:
The authors would like to thank Williams College and Steven Miller for organizing the SMALL 2024 REU.Funding:
The authors received financial support from NSF Grant DMS-2241623.Editor:
John IaconoSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
The mathematics of puzzle solving is often categorized as recreational, but it also provides foundational educational experiences. For example, many students in Computer Science and Mathematics first learn about recursion, induction, and exponential growth through the Towers of Hanoi puzzle [11]. More specifically, the standard approach of solving the puzzle involves recursively moving the smallest discs, followed by moving disc once, followed by recursively moving the smallest discs again, as illustrated in Figure 1. The number of moves in this solution is given by the recurrence in (1).
| (1) |
Students can then use induction to prove that is a closed formula for (1). This makes it an exponential puzzle since the shortest solution grows exponentially with each piece. More specifically, it is a -ary puzzle as it grows like . Alternatively, it can be seen as a -ary puzzle as its total number of states (and longest solution) is as seen in Figure 2(a).
While these conclusions often mark the end of a student’s investigation of the Towers of Hanoi, the puzzle can lead to much deeper discoveries in combinatorics and computation. For example, students may observe that the puzzle has states, but only of them are used in the shortest solution. There is a natural, but not immediately obvious, approach to encoding these states as an -bit binary string. This leads to the well-known order of binary strings known as the binary reflected Gray code [8] and its rich generalizations [13]. Similarly, the sequence of discs that move in the shortest solution follow the binary ruler sequence (Oeis A001511) or more precisely the paper folding sequence (Oeis A164677). Given an arbitrary state of the puzzle, students may wonder if they can directly determine the next state without any additional context, or how many steps it will take to reach the solution. These questions lead to the development of the successor rules and ranking algorithms for the Gray code which are covered in The Art of Computer Programming [12] and elsewhere [18, 11]. In other words, the development of (1) and its closed formula are just the first lessons provided by Towers of Hanoi. Furthermore, a wide variety of other exponential puzzles have been constructed over the years that follow a similar recurrence or associated sequences. This family of puzzles is sometimes known as Gray code puzzles.
This paper considers the new “Ziggu” family of exponential puzzles and various facets involved in solving them. For example, this family includes the Ziggustretch puzzle which is illustrated in Figure 3. As we will see, the shortest solution to all of the puzzles in the family follow a similar but more complicated recurrence.
| (2) |
Roughly speaking, solving a puzzle with Ziggu mazes requires solving its first Ziggu mazes three times, however, the solution to its first Ziggu mazes can be skipped in the latter two sub-solutions; see Figure 12(b). The recurrence in (2) has a closed form of (Oeis A101946), whose dominant term makes it a -ary exponential puzzle in terms of its shortest solution. Just like the Towers of Hanoi, we’ll see that the puzzles can also be viewed as -ary puzzles as they have (Oeis A003462) total states and solutions of this length. In fact, we’ll also view them as -ary puzzles due to an intimate connection with the quaternary (base-) reflected Gray code.
1.1 New Results
Those who have held a Ziggu puzzle may have stumbled upon this approach to exploring its configurations: modify the leftmost piece that does not undo the most recent modification. Pleasantly, this approach will eventually solve the puzzle. Moreover, it does so in as few moves as possible. However, it isn’t immediately clear how many moves are made during the solution. We prove that exactly states are on the shortest solution, and we refer to these states as Ziggu states or strings. Furthermore, if one replaces “leftmost” with “rightmost” then the exploratory approach again solves the puzzle, and it does so in as many moves as possible. In fact, this longest solution visits every one of the states, and it is the only solution to do so. Both of these formulae assume that state is encoded as one of the quaternary (i.e., base ) strings with digits, as explained later on. These three formulae are summarized in Table 4 and they explain why Ziggu puzzles can be considered to be -ary, -ary, or -ary exponential puzzles.
These results imply that Gray code puzzles are simpler than Ziggu puzzles. This is because Gray code puzzles can typically be solved using a similar exploratory approach: modify the only piece that does not undo the most recent modification. In fact, in most Gray code puzzles there is never a choice of which piece to modify (so long as the previous modification is known) and this approach gives the only solution.
We show that the connection between Ziggu puzzles and Gray code puzzles runs even deeper. In Gray code puzzles each state can be represented by an -bit binary string, and the next state is obtained by flipping the bit according to the binary reflected Gray code. Similarly, the longest and shortest solutions to Ziggu puzzles can be understood using the quaternary (i.e., base ) reflected Gray code. More specifically, the longest solution is obtained by skipping over invalid encodings in the quaternary reflected Gray code, and the shortest solution is obtained by additionally skipping over non-Ziggu states in the longest solution. This is illustrated in Figure 4 and the gaps in Table 1.
The aforementioned results show how to solve Ziggu puzzles from the start configuration to the target configuration in one sitting. However, a solver may wish to leave the puzzle in a state that is part way through the longest or shortest solution. To help with these situations we provide -time comparison and successor algorithms, which give the relative order of two states and the next state, respectively.
A preliminary version of this paper has additional material [7] including ranking and unranking (see Section 11). Its longer title also pays homage to an article by Martin Gardner [5] (see also [6]) which discusses the Gray code and several of the aforementioned puzzles.
| Quaternary Gray code | Longest solution | Shortest solution | Mazes | ||||||||||
| maze | dials | ||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
|
|
|||||||||||||
1.2 Outline
Section 2 provides background on Gray code and Ziggu puzzles. Section 3 gives a non-technical description of how to solve Gray code and Ziggu puzzles. Section 4 explains our Ziggu maze abstraction and how the mazes are formed in Zigguflat and Zigguhooked. Section 5 derives recursive formulae for shortest and longest solution lengths. Section 6 cover the binary and quaternary reflected Gray codes. Section 7 provides recursive descriptions of Gray code and Ziggu puzzle solutions. Sections 8 and 9 present the successor rules and comparison formulae for the solutions to these puzzles. Section 10 presents loopless algorithms for generating the shortest solution to Ziggu puzzles. We end with final remarks in Section 11. Omitted proofs follow from first principles or appear in [7].
2 Sequential Puzzles
This section discusses two families of sequential puzzles: Gray code puzzles and Ziggu puzzles. For a broader catalog of -ary puzzles see Goetz Schwandtner’s Extremely Puzzling! site [19].
2.1 Gray Code Puzzles: Chinese Rings, Towers of Hanoi, Spin-Out …
The most famous family of exponential puzzles is the Gray code puzzle family, which are related to the well-known pattern of binary numbers or words known as the binary reflected Gray code (BRGC) or simply “the” Gray code [13]. A sampling appears in Figure 5.
| puzzle | base | pieces | start state | target state | shortest solution | ||
| Spin-Out | binary | ||||||
| The Brain | binary | ||||||
| B-Bar | binary | ||||||
| Baguenaudier | binary | varies | Oeis A005578 | ||||
| Towers of Hanoi |
|
varies |
The family contains vintage puzzles including Spin-Out (see [16] and [3]) and The Brain. In these puzzles the underlying state space or state graph is simply a path labeled by the Gray code. The ends of the path are and and correspond to the first and last words in the Gray code. The state also features prominently as it is the start or target state in many of the puzzles. For example, the state space of The Brain with pieces appears in Figure 2(b) and the state space for Spin-Out with dials appears in Figure 11(a).
Most famously, the family contains two historical puzzles. In the Towers of Hanoi the Gray code provides the shortest solution of length among its states, and in Baguenaudier (or “Chinese Rings”) [17] the state space is the Gray code except that some pairs of moves can be made simultaneously.
Perhaps the most direct implementation of a Gray code puzzle is B-Bar by Felix Davis. As in The Brain, its state space is the path formed by , however, in this case the puzzle begins and ends at the two extreme nodes. More broadly, Hamilton paths have been used as the basis for other sequential puzzles, including Hamilton’s Icosian game [15, 14].
The Gray code puzzle family also includes puzzles in which solutions are variations of the Gray code. For example, Rudenko’s Disc can be seen as a variation of the Towers of Hanoi. More specifically, the rule that larger discs cannot be placed on top of smaller discs is replaced by a physical limitation that disc can only be pushed down positions from the top of each channel. In other words, disc can have at most discs on top of it, but the discs on top can have any number. As a result, the binary reflected Gray code provides a solution, but not the shortest. Panex Jr and Stacker by CoreMods further refine this idea by limiting the height of one of the channels (i.e., pegs) thereby lengthening the solutions.
The Towers of Bucharest restricts the Towers of Hanoi by not allowing discs to move directly between the left and right pegs. The puzzle is also known as Linear Towers of Hanoi. Regardless of its name, the unique solution visits all states in ternary Gray code order [20, 10, 11], and so it is also the longest possible solution to the Towers of Hanoi (see Figure 2). Another puzzle that follows the ternary Gray code is Crazy Elephant Dance [2].
2.2 Ziggu Puzzles: Ziggurat, Zigguflat, Zigguhooked …
The Ziggurat puzzle was created by Eitan Cher and Bram Cohen and submitted to the 2021 Puzzle Design Competition [1]. The puzzle is notable for several reasons. It is an exponential puzzle since it consists of some number of (nearly) identical pieces and the number of steps required to solve the puzzle grows exponentially in some base. Unlike previous puzzles of this type it is a burr meaning that the goal is to fully disassemble (or reassemble) it. It is also frameless meaning that the puzzle consists only of its pieces without any other structure holding it together.111Note that the black base in Figure 6(a) is simply a stand and is not part of the puzzle. The name of the puzzle is based on how the pieces are initially stacked into a 3-dimensional pyramidal configuration resembling a ziggurat temple. The underlying maze mechanic underpinning the puzzle is discussed in Section 4.
| puzzles | type | pieces | mazes | digits | shortest solution | longest solution |
| Ziggurat, Zigguflat, Ziggutrees, Ziggustretch | 4-neighbor | 6 | 4 | 5 | 172 | 364 |
| Zigguchain, Zigguhooked, Ziggutwist, Ziggutwist Too | 2-neighbor | 6 | 5 | 6 | 361 | 1093 |
| strings | set | cardinality | growth | values | sequence |
| encoding | Oeis A000302 | ||||
| valid state | Oeis A003462 | ||||
| ziggu | Oeis A101946 |
Oskar van Deventer implemented the same underlying maze structure in a variety of creative ways in subsequent puzzles including Zigguflat, Ziggutwist, Ziggutwist Too, Zigguchain, Zigguhooked, Ziggutrees, and Ziggustretch. All of these puzzles are seen in Figure 6 with pieces. In some of these puzzles the mazes can be clearly seen (e.g., Zigguflat) while in others they are partially obscured (e.g., Ziggutwist) or fully obscured (e.g., Ziggustretch). Ziggu puzzles are typically color-coded so that the red piece is the only one that can be modified in the starting configuration. Right-handed solvers often hold the puzzle with this piece on the right. We’ll assume this orientation in our discussions, and we’ll also refer to the red piece as the first piece, the orange piece as the second piece, and so on. (In Ziggurat the bottom piece is the first piece.)
While each Ziggu puzzle is unique, they fall into two primary categories, summarized in Table 3. Ziggurat, Zigguflat, Ziggutrees, and Ziggustretch (Figures 6(a)-6(d)) are all 4-neighbor puzzles, meaning that each piece touches at most four other pieces. In particular, Figure 6(b) shows that the yellow piece touches the orange and red pieces in one direction, as well as the blue and green pieces in the other direction. On the other hand, Zigguchain, Zigguhooked, Ziggutwist, and Ziggutwist Too are all 2-neighbor puzzles, as each piece touches up to two other pieces. This is easily seen in Figures 6(e)-6(h), where the pieces are arranged linearly and each piece touches its left and right neighbor.
In the remainder of this paper, we consider Zigguflat and Zigguhooked as examples of 4- and 2-neighbor puzzles. These have the advantage of using distinctly different implementations of the Ziggu maze while still being planar, which makes the mechanics visible. As noted in Table 3, while both Zigguflat and Zigguhooked consist of pieces, Zigguhooked will take longer to solve since it has mazes, while Zigguflat has only mazes.
Despite these seemingly large differences, all of the Ziggu puzzles are identical in an abstract sense. More specifically, all Ziggu puzzles with mazes have identical state spaces and solutions.
3 Solving Puzzles from the Start
In this section, we give simple approaches to solving Gray code puzzles and Ziggu puzzles from their start states. The content is tailored to those who wish to solve a puzzle once as simply as possible. We assume the solver begins from the start state and knows their previous move. Subsequent sections in this paper formalize these results and address more nuanced questions and scenarios.
3.1 Don’t Look Back!
Gray code puzzles and Ziggu puzzles can be solved using the same basic principle: don’t look back! This means that you should never reverse your progress by undoing your most recent move. This advice is enough to solve many Gray code puzzles due to the following: Gray code puzzles have at most one non-reversing move. In other words, when you move to a new state there is at most one move that doesn’t take you back to your previous state. In some puzzles (e.g., Spin-Out) the only complication is that there are two different moves that can be made from the start state; the wrong first move leads toward a dead-end from which you need to undo your final move and then never look back. That is, the state space is a path and only one direction takes you to the solution. For example, the neighbors in Spin-Out222A design flaw in the initial run of Spin-Out puzzles introduces more states and shorter solutions by allowing dials on the right to be rotated to the right. This can be fixed by extending the right end of the frame. Our discussion of Spin-Out ignores the flaw. are illustrated in Figure 7. In Towers of Hanoi there can be two non-reversing moves, but this reduces to one when putting additional restrictions on how and when to move the smallest disc.333On every odd numbered turn, move disc one peg to the left (right) for even (odd) .
Ziggu puzzles are more challenging than Gray code puzzles due to the following: Ziggu puzzles can have more than one non-reversing move; hence Ziggu puzzles have solutions of different lengths. A puzzle with mazes and digits can have up to non-reversing moves444For example, the state (see Section 4) allows changes to each and the last .,555The shortest solution only visits states with up to two non-reversing moves. In this sense, Ziggu puzzles remain relatively simple until you go off course.. Figure 8 gives an example of such a state in Zigguhooked. While Ziggu puzzles have more complicated states, the unique shortest and longest solutions are generated by simple extremal versions of the basic don’t look back principle: For the shortest/longest solution, always make the leftmost/rightmost non-reversing move. Since there is only one available move from the start state, you always begin by moving in the correct direction. Besides the start state, the only other “dead end” is the solution state. As a result, you’ll never need to backtrack over previous moves. However, you can return to a previous state by going around in a circle (i.e., through a cycle of states) if you don’t follow either of the extremal versions of the basic principle.
3.2 One Bit to Remember: Resuming a Shortest Solution
By design exponential puzzles take a long time to solve, and solvers may wish to take a break. Trouble will ensue if a solver resumes by undoing the last move they made before taking a break. This can be avoided by recording the last move made. In fact, less information is needed: we only one bit of information. One literal bit.
Gray code puzzles have a “metronome” property wherein every second change involves the first piece. The change sequence follows the binary ruler sequence: (Oeis A001511). So you only need to remember if your most recent move involved the first piece or not. The metronome property holds for puzzles that start in an intermediate state (e.g., Spin-Out) although the change sequence is a (reversed) subsequence of Oeis A001511.
The shortest solution to Ziggu puzzles instead have a “stair-climbing” property wherein successive changes differ by at most one: . Suppose that you always make the moves in succession (i.e., when changing the first piece you do it three times in a row). Treating each as a single is natural in most of the Ziggu puzzles (e.g., the first dial in Zigguhooked moves freely between its two extreme states and ). With this assumption you just remember if your most recent move went up (e.g., then ) or down (e.g., then ). In the first case you will attempt to move up again (e.g., then then ); if the puzzle does not allow this, then you will move down (e.g., then then ). In the second case you move down again (e.g., then then ) or repeatedly move the first piece.
Here is a general tip to avoid undoing your work in a Gray code or Ziggu puzzle: Never pause after changing the first piece or going down.
4 Ziggu Mazes, Pieces, and Digits
While the Ziggu family consists of a wide family of seemingly distinct puzzles, they all use the same underlying mechanic of the Ziggu maze.
4.1 Ziggu Mazes
Definition 1.
A Ziggu maze is the -shaped maze in Figure 9. It lives on a grid, and the 13 valid positions are
| (3) |
The rows and columns are labeled from top to bottom and from right to left, respectively. The 13 valid states are connected as shown in Figure 9. The initial state of the maze is , and the maze’s exit is accessible from .
An -Ziggu maze is a sequence of Ziggu mazes in states
| (4) |
That is, the horizontal position of a maze is the vertical position of the maze to its right.
The base representation of an -Ziggu maze is the length base string .
For example, the -Ziggu maze with state has the maze representation . While there are digits in the state description, since and , we can describe the state of this -Ziggu maze with digits with the base string .
Definition 1 immediately gives a bijection between the set of -Ziggu mazes and the set of length base strings in which is the only digit appearing to the right of a .
4.2 Implementation of Ziggu mazes in the puzzles
The first puzzle in the Ziggu family was Ziggurat. Each Ziggurat piece contains a Ziggu maze, and successive pieces have alternating chirality. For example, the blue pieces in Figure 6(a) have an -shaped maze and the orange pieces have an -shaped maze. The bottom of each piece contains a nub that navigates the maze two pieces below it. For example, the nub of the top blue piece passes through the top orange piece and stays within the maze of next blue piece. The puzzle is solved when every maze is solved (i.e., every nub reaches the end of its respective maze) which allows it to be disassembled. The challenge comes from the fact that consecutive pieces move in lockstep in one of the two directions of the maze. This is accomplished by the railing at the bottom of each piece slotting into the channel of the piece below it, as shown on the right side of 6(a). As a result, making progress in one maze can involve moving to the opposite end of the previous maze, and this cascade of back-and-forth movements gives the puzzle its exponential nature. The Ziggurat puzzle inspired Oskar van Deventer to create the vast family of Ziggu puzzles shown in Figure 6. These puzzles all use the Ziggu maze, but they feel distinct and provide differing enjoyable puzzling experiences.
The Zigguflat puzzle consists of interlocking pieces as shown in Figure 6(b). The mazes are cut into the green, yellow, orange, and red pieces, and those mazes are navigated by -shaped nubs of the purple, blue, green, and yellow pieces, respectively. Therefore Zigguflat has mazes. Notice that the orange and red pieces only do not have -shaped nubs and the purple and blue pieces do not have mazes so that the fully assembled puzzle is a rectangle. As the commercial version of Zigguflat has mazes, it requires digits to describe its state. See Figure 10(a) for an example. To read off the digits describing the state of the commercial Zigguflat puzzle with 6 pieces, the first four digits describe the vertical positions of the purple, blue, green, and yellow nubs within the mazes they navigate, respectively. The final fifth digit describes the horizontal position of the yellow nub within the red maze.
The Zigguhooked puzzle consists of interlocking dials with in the commercially available version (see Figure 6(f).) Each maze is formed at the interface of two consecutive dials, so there are mazes in total. The rotation of the dials are described by digits. Given a pair of adjacent dials, rotating the left dial changes the vertical position within the maze, while rotating the right dial changes the horizontal position within the maze. One can also read the digits off of the dials without examining the mazes. The radial positions are numbered increasing in a counterclockwise direction, where the states , , , and correspond to the arrow pointing in the approximately 3:45, 3:15, 2:45, and 2:15 directions of the hour hand on a clock face, respectively. So a Zigguhooked puzzle in the state would have its dials in position . See Figure 10(b) for an example.
5 Recurrences and Closed Formulae: Solution Lengths
In this section, we consider how many moves it takes to solve a puzzle by using recursive formulae that express the number of moves in terms of the number of moves that solve smaller versions of the puzzle. In other words, we repeat the classic approach to analyzing the Towers of Hanoi (see Figure 1) to other Gray code puzzles and Ziggu puzzles. In each case, we consider the number of moves and the number of states on a solution separately. These two quantities are closely related – the number of states is one more than the number of moves (i.e., ) – but the recursive formulae arrive at these values in different ways. We also argue that these recurrences give the unique shortest (or longest) solutions. The recurrences are summarized in Tables 5–6.
5.1 Gray Code Puzzle Solution Lengths
As was shown in Table 2, most Gray code puzzles run between states that are encoded as and , or between and (or vice versa).
We previously saw the recurrences for the number of moves and the number of states required to solve the Towers of Hanoi in Section 1. These holds for B-Bar as well. The two recurrences are provided in the top of Table 5. Theorem 2 substitutes in closed forms of these recurrences.
Theorem 2.
The number of states in the shortest solution to the Towers of Hanoi with discs is . The number of moves on the shortest solution is . These quantities count the number of strings between and in the binary reflected Gray code.
Recurrences for the number of moves and the number of states required to solve Spin-Out appear in Figure 11(b). These hold for Baguenaudier and The Brain as well. The two recurrences are provided in the bottom of Table 5. Theorem 3 substitutes in closed forms of these recurrences.
Theorem 3.
The number of states in the shortest solution to Spin-Out with dials is . The number of moves on the shortest solution is These quantities count the number of strings between and in the binary reflected Gray code.
| puzzle | quantity | recurrence | base cases | closed form | sequence |
| Towers of Hanoi | moves | Oeis A000225 | |||
| Towers of Hanoi | states | Oeis A000079 | |||
| Spin-Out | moves | Oeis A000975 | |||
| Spin-Out | states | Oeis A005578 |
| quantity | recurrence | base cases | closed form | sequence |
| most moves | Oeis A029858 Oeis A123109 | |||
| most states | Oeis A003462 | |||
| fewest moves | N/A | |||
| fewest states | Oeis A101946 |
5.2 Ziggu Puzzle Solution Lengths
Ziggu puzzles always have the same start state and target state, so these factors do not result in different recurrences. However, these puzzles have different shortest and longest solutions, so we will still develop two different pairs of recurrences. The principle behind our recurrences can be found in Figure 12. More specifically, this figure shows how the shortest and longest solutions in a Ziggu maze puzzle can be expressed in terms of the solutions to smaller Ziggu maze puzzles. Figure 13 and 13 then illustrates the recurrences for the shortest and longest solutions in Zigguhooked, respectively. All four recurrences and summarized in Table 6. Theorem 4 substitutes in closed forms of the respective formulae.
Theorem 4.
The number of states in the shortest and longest solutions to Ziggu puzzles with digits is and , respectively. These quantities count the number of Ziggu and valid strings between and in the quaternary Gray code, respectively. The number of moves is given by
6 Reflected Gray Codes: Binary and Quaternary
In this section we discuss the Gray codes that underlie Gray code puzzles and Ziggu puzzles. The term Gray code can broadly refer to a listing of some set of objects in which consecutive objects differ in a small amount, and in this section we’ll consider Gray codes for -bit binary strings and -digit quaternary strings. Both Gray codes use reflection, which reverses a list of strings (but not the individual strings themselves). For example, .
When ordering numbers or strings, Gray codes are not the same as standard numeric or lexicographic order. For example, in standard numeric order the -bit binary string is followed by (i.e., all bits are flipped) whereas it is followed by (i.e., only the second rightmost bit is flipped) in the Gray code in Section 6.
The most well-known Gray code is the binary reflected Gray code (BRGC) or simply the Gray code. It is an ordering of all binary strings of length in which successive strings differ in only a single bit. The orders for and are provided below, where the overlines mark the bits that change to create the next string.
| (5) |
The global structure of the binary reflected Gray code is given by the following recursive formulae.
| (6) |
In this formula, the denotes concatenation, the commas denote appending one list to another, and R denotes the list in reflected order. For example, the first four words of are identical to except that is prefixed to them. Similarly, the last four words of are identical to written in reflected order and with prefixed. The formula (6) and its resulting order are usually attributed to Frank Gray [8], but their histories go back much further [9].
Its worth noting that the Gray code can also be created using a simple greedy algorithm [24]. Start a list with the all-zeros string (where exponentiation denotes repetition) and then repeatedly create the next string in the list from the most recently added string as follows: complement the rightmost bit that creates a new string. For example, when the list will begin . To create the next string we try complementing the bits in from right-to-left: is already in the list; is already in the list; is not in the list so it is added to the end of the list. This notion of not recreating a previous string is similar to the puzzle-solving approach from Section 3.2.
We define the quaternary reflected Gray code recursively as follows.
| (7) |
Note that the global structure of the quaternary reflected Gray code is very similar to the structure of the binary reflected Gray code, as both formulas involve prepending digits to the list of one size smaller, with every second copy in reverse order.
7 Recursive Formulae: The Global Structure of Solutions
Understanding how the solution of a puzzle changes as the number of mazes in the puzzle changes requires knowledge of the global structure of the solution. We describe the global structure of a sequence using a recursive formula, where the formula for a given is built up from the sequences for smaller .
The state space of many Gray code puzzles is precisely a path given by the binary reflected Gray code whose recursive description is given by (6).
The recursive structure of the longest solution to Ziggu puzzles is given by the following.
| (8) |
This recursive formula is derived from the recursive formula for the quaternary reflected Gray code, with the modification that instead of recursively including the entire list , we append the state , as there is no need to continue beyond the solved state . See Figures 14 and 15.
The recursive structure of the shortest solution to Ziggu puzzles is slightly more complicated. Let , and for , let
| (9) |
The denotes all but the first element of . This “list slicing” is needed because the word appears as both the last word of and the first word of is the sublist of obtained by omitting all states where is followed by a digit that is not .
8 Successor and Predecessor Rules: The Next and Previous States
Puzzles such as the Gray code and Ziggu puzzles can take a long time to solve, and so it is conceivable that a puzzle solver might set a puzzle down during a solving session to say, make a cup of tea. If the puzzle solver noted their last move or could recall it, they may be able to resume their solution using the approach outlined in Section 3.2. However, if a puzzle solver has no knowledge of the previous move, they would use a successor rule to determine the next state to which they should advance the puzzle. Similarly, a predecessor rule determines the previous state in the list.
Let be an even base (binary is , quaternary is ), and let be a base- string of length . Its reflected Gray code successor is obtained by incrementing or decrementing digit as follows
| (10) |
where is the minimum index such that one of the two cases holds. If there is no such value of , then ’s successor is undefined.
For the quaternary () string , we see that since and is even. (10) gives that , so digit was incremented.
If is undefined for a quaternary string , then , which is the last string in the quaternary Gray code
We now provide explicit successor rules for the longest and shortest solutions to Ziggu puzzles. These are sublists of the quaternary reflected Gray code, so we work over base , no longer over an arbitrary even base . Let be a long quaternary string of length . Its long successor is obtained by incrementing or decrementing digit as follows
| (11) |
where is the minimum index such that one of the two cases holds. If there is no such value of , then ’s long successor is undefined.
For example, if , then , since and is odd. Therefore , as digit was decremented.
Comparing (10) and (11), we see that the longest solution omits the strings in the quaternary reflected Gray code where a is followed by digits that are not .
The successor rule for the shortest solutions adds an extra condition. Let be a short quaternary string of length . Its short successor is obtained by incrementing or decrementing digit as follows
| (12) |
where is the minimum index such that one of the two cases holds. If there is no such value of , then ’s short successor is undefined.
For example, let . We see that now since . Instead, since and is even. Therefore , where we incremented digit .
Comparing (11) and (12), we see that the shortest solution omits the strings in the longest solution where a is followed by digits that are not .
If or is undefined for or , then , which is the last string in both the longest and shortest solution.
9 Comparison: The Relative Position of States
Determining which moves will advance a puzzle in an arbitrary state towards the initial or solved state can seem quite daunting. In Gray code puzzles where the initial state is but the puzzle can have different valid states that also have first digit , one may have to “backtrack” in the Gray code to find the shortest path to the official starting state of the puzzle. A comparison formula allows a puzzle solver to determine the relative order of the current state and the solved state, thus indicating whether or not to proceed forwards or backwards in the Gray code.
For the Ziggu puzzles where the initial state and the solved state are the first and final valid states in both the longest and shortest solutions and , a comparison formula is more generally useful when one is curious about the relative order of two puzzle states.
We present a comparison formula that can compute the relative order of two states in all four Gray codes we’re considering: the binary and quaternary reflected Gray codes, and the longest and shortest solutions to the Ziggu puzzles.
Given two distinct states with and , we can determine if occurs before in the binary reflected Gray code as follows. Let be the leftmost digit where and differ, i.e, is the index such that for all and We compute that occurs before as follows.
| (13) |
For example, in , since , is odd, and . The parity condition on the sum of the digits to the left of captures the number of times that the recursive sublists are written in reverse order.
The comparison in (13) also gives the relative order for distinct states and in the quaternary reflect Gray code , as the parity condition still captures this same sublist reversal behavior. For example, in since , is even, and .
Since both the longest and shortest solutions are sublists of , we see immediately that if we are given distinct states or then (13) provides their relative order within these respective lists.
10 Loopless Algorithms for Ziggu Puzzles
Combinatorial generation refers to the efficient generation of every object of a particular type [18]. In this context, the best type of algorithm is a loopless algorithm, which creates each successive object in worst-case -time. More specifically, a single object is stored in memory, and the generation algorithm makes a constant-sized modification to obtain the next object. Loopless algorithms were pioneered by Ehrlich [4] and exist for most common types of combinatorial objects (e.g., all -bit binary strings, all permutations of , etc.). In this section we provide loopless algorithms for the shortest solutions to Ziggu puzzles.
We present a loopless algorithm for generating the shortest solution to Ziggu puzzles, provided one stores a “direction vector,” in Algorithm 1. Algorithm 1 has the following initial state: initial word and starting index . The array of direction is initialized to have every entry be a , indicating that each digit should be increased (lines 2-4).
In every loop iteration, Algorithm 1 changes by the value , the -th right-to-left -indexed value in the array (line 7). If this change results in being extremal, so equals or , is set from increasing to decreasing or vice versa (line 9). In the next loop iteration, the digit to the left of will be changed (line 10). If was instead changed to a nonextremal value ( or ), then the next loop iteration will consider the digit to the right of , if is not the rightmost digit (lines 11-12). If is the rightmost digit, will be changed again in the next loop iteration.
We remark that the memory required for this algorithm can be reduced, as the storage of the directions is not strictly necessary. However, storing the direction vector simplifies the algorithm.
11 Final Remarks
Preliminary results in this paper were shared by the second author at a Gathering for Gardner – Celebration of Mind presentation. This led to several enjoyable communications with the community including Bram Cohen, Oskar van Deventer, and Goetz Schwandtner. Along with Hirokazu Iwasawa they have been making parallel investigations into Ziggu puzzles and their generalizations. In particular, they have considered variations to the basic -shaped maze and how this changes the solution lengths. This research has resulted in new puzzle designs, including van Deventer’s recent Zigguphi puzzle. The authors look forward to playing these new puzzles, and hope that some of the results presented here will help in their analysis. For those who would like to try their hand at a physical copy of these puzzles, please consult van Deventer’s webpage of freely available 3D-printing files [23] or visit an online puzzle retailer.
The authors of this paper have noticed that our results generalize to mazes that continue the back-and-forth pattern (i.e., rows with ). For further technical results, including change sequences, ranking algorithms, an additional algorithm for generating the shortest solution, and a bijection with the pencil-and-paper puzzle Nurikabe, see the preliminary version [7] of this paper.
References
- [1] Puzzle Design Competition Committee. 2021 Puzzle Design Competition Entries. URL: https://johnrausch.com/DesignCompetition/2021/.
- [2] Curtis Cooper. The Chinese ring puzzle, the crazy elephant dance puzzle, the b-spinout puzzle, and Gray codes. The Mathematical Gazette, 103(558):431–441, 2019.
- [3] Paul Cull, Leanne Merrill, and Tony Van. A tale of two puzzles: Towers of Hanoi and Spin-Out. Information and Media Technologies, 8(3):695–709, 2013. doi:10.11185/IMT.8.695.
- [4] Gideon Ehrlich. Loopless algorithms for generating permutations, combinations, and other combinatorial configurations. J. ACM, 20(3):500–513, 1973. doi:10.1145/321765.321781.
- [5] Martin Gardner. Curious properties of the Gray code and how it can be used to solve puzzles. Scientific American, 227(2):106–109, 1972.
- [6] Martin Gardner. Knotted doughnuts and other mathematical entertainments. AMS, 2020.
- [7] Madeleine Goertz and Aaron Williams. The quaternary Gray code and how it can be used to solve Ziggurat and other Ziggu puzzles. arXiv preprint arXiv:2411.19291, 2024.
- [8] Frank Gray. Pulse code communication, March 1953.
- [9] FG Heath. Origins of the binary code. Scientific American, 227(2):76–83, 1972.
- [10] Felix Herter and Günter Rote. Loopless Gray code enumeration and the Tower of Bucharest. Theoret. Comput. Sci, 748:40–54, 2018. doi:10.1016/J.TCS.2017.11.017.
- [11] Andreas M Hinz, Sandi Klavžar, Uroš Milutinović, and Ciril Petr. The Tower of Hanoi-Myths and Maths. Birkhäuser, 2nd edition, 2018.
- [12] Donald E Knuth. The art of computer programming, volume 4A: combinatorial algorithms, part 1. Pearson Education India, 2011.
- [13] Torsten Mütze. Combinatorial Gray codes - an updated survey, 2023. arXiv:2202.01280.
- [14] Torsten Mütze. On Hamilton cycles in graphs defined by intersecting set systems. Notices Amer. Math. Soc., 71(5), 2023.
- [15] Ed Pegg Jr. The icosian game, revisited. The Mathematica Journal, 11(3):310–314, 2009.
- [16] Kirk Pruhs. The Spin-Out puzzle. ACM SIGCSE Bulletin, 25(3):36–38, 1993. doi:10.1145/165408.165421.
- [17] Lisa Rougetet. Le binaire au bout des doigts: Un casse-tête entre récréation mathématique et enseignement. UGA Éditions, 2024.
- [18] Frank Ruskey. Combinatorial generation. Preliminary working draft. University of Victoria, Victoria, BC, Canada, 11:20, 2003.
- [19] Goetz Schwandtner. -ary puzzles, November 2025. URL: https://puzzles.schwandtner.info/group_nary.html.
- [20] RS Scorer, Patrick Michael Grundy, and Cedric Austen Bardell Smith. Some binary games. The Mathematical Gazette, 28(280):96–103, 1944.
- [21] Paul K Stockmeyer. An exploration of sequence A000975. Fibonacci Quart., 55(5):174–185, 2017.
- [22] Jim Storer. Spinout. URL: https://www.cs.brandeis.edu/˜storer/JimPuzzles/ZPAGES/zzzSpinout.html.
- [23] Oskar van Deventer. Print it yourself. https://oskarvandeventer.nl/Print-It-Yourself/, 2022.
- [24] Aaron Williams. The Greedy Gray Code Algorithm. In Frank Dehne, Roberto Solis-Oba, and Jörg-Rüdiger Sack, editors, Algorithms and Data Structures, pages 525–536, Berlin, Heidelberg, 2013. Springer. doi:10.1007/978-3-642-40104-6_46.
