Abstract 1 Introduction 2 Sequential Puzzles 3 Solving Puzzles from the Start 4 Ziggu Mazes, Pieces, and Digits 5 Recurrences and Closed Formulae: Solution Lengths 6 Reflected Gray Codes: Binary and Quaternary 7 Recursive Formulae: The Global Structure of Solutions 8 Successor and Predecessor Rules: The Next and Previous States 9 Comparison: The Relative Position of States 10 Loopless Algorithms for Ziggu Puzzles 11 Final Remarks References

The Quaternary Gray Code and Ziggu Puzzles

Madeleine Goertz ORCID Department of Mathematics, University of California, Berkeley, CA, USA Aaron Williams ORCID Department of Computer Science, Williams College, Williamstown, MA, USA
Abstract

We investigate solutions to the new “Ziggu” family of sequential puzzles including Ziggurat, Zigguflat, Zigguhooked and so on. These puzzles have p pieces that form m mazes. We encode the state of each puzzle as a quaternary number (i.e., base 4) with n=m+1 digits, where each digit gives the horizontal or vertical position in one maze. For example, the commercial version of Zigguflat has p=6 pieces connected into m=4 mazes and its state requires n=5 digits to describe. We show that the number of states on a shortest solution is 62n3n5 (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 (3n+11)/2 states (Oeis A003462). In this way, Ziggu puzzles can be viewed as 4-ary, 3-ary, or 2-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 p pieces typically have 2p (Oeis A000079) or 232p (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 O(n)-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 Algorithm
Funding:
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:
[Uncaptioned image] © Madeleine Goertz and Aaron Williams; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Mathematics of computing Combinatorial algorithms
; Mathematics of computing Combinatorics on words
Related Version:
For additional technical results, see the preliminary version of this article.
Preliminary Version: https://arxiv.org/abs/2411.19291 [7]
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 Iacono

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 n1 discs, followed by moving disc n once, followed by recursively moving the smallest n1 discs again, as illustrated in Figure 1. The number of moves in this solution is given by the recurrence in (1).

T(n)=2T(n1)+1 with T(1)=1. (1)

Students can then use induction to prove that 2n1 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 2-ary puzzle as it grows like 2n. Alternatively, it can be seen as a 3-ary puzzle as its total number of states (and longest solution) is 3n as seen in Figure 2(a).

(a) Recursively move the smallest n1 discs to the middle, then move disc n to the right, then recursively move the smallest n1 discs to the right. The number of moves is 2n1 by the recurrence f(n)=2f(n1)+1 with f(1)=1, while the total number of states is 2n by the recurrence g(n)=2g(n1) with g(1)=2.
(b) The shortest solutions for n=2 and n=3 move discs 1,2,1 and 1,2,1,3,1,2,1 respectively.
Figure 1: Towers of Hanoi starts with n discs on the left peg and ends with them on the right. The top disc on a peg can move to the top of another peg but not onto a smaller disc. The shortest solution is in (a) and is illustrated in (b). It is a Gray code puzzle as the binary ruler sequence gives the disc moves (where disc i moves cyclically left/right if it has the same/opposite parity as n) and the binary reflected Gray code 𝖡𝖱𝖦𝖢(n) gives the states if they are encoded in binary bnbn1b1.

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 3n states, but only 2n of them are used in the shortest solution. There is a natural, but not immediately obvious, approach to encoding these states as an n-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.

(a) The Towers of Hanoi state graph for n=2 discs. A longest solution and the unique shortest solutions are highlighted.
(b) The Brain state graph for n=4 pegs. The unique solution is highlighted. There is only one move from the start state 0000 and the moves follow the ruler sequence 1,2,1,3,.
Figure 2: State graphs of two Gray code puzzles (a) the Towers of Hanoi and (b) The Brain. In the Towers of Hanoi there are 3n states and they are most naturally encoded using ternary strings (i.e., in tntn1t1 the digit ti provides the peg on which disc i is located). A longest solution visits every state in ternary reflected Gray code order. Furthermore, discs only move between neighboring pegs so it solves the Towers of Bucharest puzzle. The unique shortest solution is a path of length 2n. Furthermore, an alternate encoding follows the binary reflected Gray code. In the alternate encoding bnbn1b1 the bit bn is 0/1 if disc n is on the left/right peg and bi is 0/1 if disc i is on top of disc i+1 for i<n. The Brain is a more conventional example of a Gray code puzzle as its state graph is simply a path following the binary reflected Gray code. As a result, the only challenge from the start state 0n to the target state 1n is avoiding undoing your previous move and going backwards. In other words, the mechanism that enforces the solution is more interesting than the solution itself!

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.

Z(n)=3Z(n1)2Z(n2)+3 with Z(1)=4 and Z(2)=13. (2)

Roughly speaking, solving a puzzle with m=n1 Ziggu mazes requires solving its first m1 Ziggu mazes three times, however, the solution to its first m2 Ziggu mazes can be skipped in the latter two sub-solutions; see Figure 12(b). The recurrence in (2) has a closed form of Z(n)=62n3n5 (Oeis A101946), whose dominant 2n term makes it a 2-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 3-ary puzzles as they have (3n+11)/2 (Oeis A003462) total states and solutions of this length. In fact, we’ll also view them as 4-ary puzzles due to an intimate connection with the quaternary (base-4) 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 62n3n5 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 (3n+11)/2 states, and it is the only solution to do so. Both of these formulae assume that state is encoded as one of the 4n quaternary (i.e., base 4) strings with n digits, as explained later on. These three formulae are summarized in Table 4 and they explain why Ziggu puzzles can be considered to be 4-ary, 3-ary, or 2-ary exponential puzzles.

(a) The puzzle starts with the pieces screwed together (state 0n) (top) and ends with them as far apart as possible (state 3n) (middle) which unlocks them (bottom).
(b) State space for p=3 pieces. States are n=2 quaternary digits q2q1 which encode the distance between adjacent pieces. Edges indicate the valid moves and are labeled by the index of the digit changed by ±1. The graph is m=1 Ziggu maze.
Figure 3: The goal of Ziggustretch is to unscrew p interlocking pieces and take them apart. Adjacent pieces are separated by a discrete distance between 03, so the state can be encoded as a quaternary (base-4) word with n=p1 digits. Hidden channels in the cylinders ensure that only one gap can be changed at a time. Furthermore, the moves are physically restricted based on m=p2 “ziggu” mazes. For example, when there are p=3 pieces the state is n=2 quaternary digits and the state space is a path that follows m=1 maze. The puzzle is more interesting with additional pieces as multiple mazes must be navigated and “shortcuts” are possible (see Figure 4).

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 n-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 4) 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 O(n)-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.

(a) State graph for n=2.
(b) State graph for Ziggu puzzles encoded with n=3 digits.
Figure 4: Ziggu puzzles with n digits have the same state space. In (a), the shortest and longest solutions are the same and consist of 13 states. In (b) the gold edges form the unique shortest solution while the additional purple edges form the unique longest solution. The gray vertices appear on the longest solution but not the shortest solution. The lengths of the shortest and longest paths are (a) 13 and 13 and (b) 34 and 40, respectively, as in Table 4. Note that (a) is a subgraph of (b) (with 0 prefixed to each state label).
Table 1: For n=3 digits, we examine the quaternary reflected Gray code n, the longest solution 𝕍n, and the shortest solution 𝕊n. For each order, we include the ranking, the Gray code, the change sequence, and the unsigned change sequence. The last two columns present the maze representation corresponding to each 2-digit base 4 string and the dials representation for the Zigguhooked puzzle. More information on ranking can be found in the preliminary version of this paper [7].
Quaternary Gray code Longest solution Shortest solution Mazes
𝗋𝖺𝗇𝗄 3 Δ3 Δ±3 𝗋𝖺𝗇𝗄𝕍 𝕍3 Δ𝕍3 Δ±𝕍3 𝗋𝖺𝗇𝗄𝕊 𝕊3 Δ𝕊3 Δ±𝕊3 maze dials
0 000 1 1 0 000 1 1 0 000 1 1

1 001 1 1 1 001 1 1 1 001 1 1

2 002 1 1 2 002 1 1 2 002 1 1

3 003 2 2 3 003 2 2 3 003 2 2

4 013 1 1 4 013 1 1 4 013 1 1

5 012 1 1 5 012 1 1 5 012 1 1

6 011 1 1 6 011 1 1 6 011 1 1

7 010 2 2 7 010 2 2 7 010 2 2

8 020 1 1 8 020 1 1 8 020 1 1

9 021 1 1 9 021 1 1 9 021 1 1

10 022 1 1 10 022 1 1 10 022 1 1

11 023 2 2 11 023 2 2 11 023 2 2

12 033 1 1 12 033 3 3 12 033 3 3

13 032 1 1
14 031 1 1
15 030 3 3
16 130 1 1
17 131 1 1
18 132 1 1
19 133 2 2 13 133 2 2 13 133 2 2

20 123 1 1 14 123 1 1 14 123 1 1

21 122 1 1 15 122 1 1 15 122 1 1

22 121 1 1 16 121 1 1 16 121 1 1

23 120 2 2 17 120 2 2 17 120 2 2

24 110 1 1 18 110 1 1 18 110 1 1

25 111 1 1 19 111 1 1 19 111 1 1

26 112 1 1 20 112 1 1 20 112 1 1

27 113 2 2 21 113 2 2 21 113 2 2

28 103 1 1 22 103 1 1 22 103 3 3

29 102 1 1 23 102 1 1

30 101 1 1 24 101 1 1

31 100 3 3 25 100 3 3

32 200 1 1 26 200 1 1

33 201 1 1 27 201 1 1

34 202 1 1 28 202 1 1

35 203 2 2 29 203 2 2 23 203 2 2

36 213 1 1 30 213 1 1 24 213 1 1

37 212 1 1 31 212 1 1 25 212 1 1

38 211 1 1 32 211 1 1 26 211 1 1

39 210 2 2 33 210 2 2 27 210 2 2

40 220 1 1 34 220 1 1 28 220 1 1

41 221 1 1 35 221 1 1 29 221 1 1

42 222 1 1 36 222 1 1 30 222 1 1

43 223 2 2 37 223 2 2 31 223 2 2

44 233 1 1 38 233 3 3 32 233 3 3

45 232 1 1
46 231 1 1
47 230 3 3
48 330 1 1
49 331 1 1
50 332 1 1
51 333 2 2 39 333 33 333

52 323 1 1
53 322 1 1
54 321 1 1
55 320 2 2
56 310 1 1
57 311 1 1
58 312 1 1
59 313 2 2
60 303 1 1
61 302 1 1
62 301 1 1
63 300

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 n-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.

Refer to caption
(a) Spin-Out [22].
Refer to caption
(b) The Brain.
Refer to caption
(c) Rudenko’s Disk.
Refer to caption
(d) Panex Jr.
Refer to caption
(e) Towers of Hanoi.
Refer to caption
(f) Baguenaudier.
Refer to caption
(g) Crazy Elephant Dance.
Refer to caption
(h) B-Bar.
Figure 5: The Gray code family of puzzles.
Table 2: The number of pieces n in various Gray code puzzles, along with the number of states in the unique (shortest) solutions. Towers of Hanoi is ternary (3-ary) in terms of the total number of states, but only binary (2-ary) in terms of its shortest solution.
puzzle base pieces n start state target state shortest solution
Spin-Out binary 7 1111111 0000000 86
The Brain binary 8 00000000 11111111 171
B-Bar binary 8 00000000 10000000 256
Baguenaudier binary varies 1n 0n Oeis A005578
Towers of Hanoi
binary
ternary
varies 0n 10n1 2n

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 0n and 10n1 and correspond to the first and last words in the Gray code. The state 1n 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 n=4 pieces appears in Figure 2(b) and the state space for Spin-Out with n=3 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 2n among its 3n 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 𝖡𝖱𝖦𝖢(n), 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 i can only be pushed down i positions from the top of each channel. In other words, disc i can have at most i1 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 3n 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.

(a) Ziggurat.
(b) Zigguflat.
(c) Ziggutrees.
(d) Ziggustretch.
(e) Zigguchain.
(f) Zigguhooked.
(g) Ziggutwist.
(h) Ziggutwist Too.
Figure 6: The Ziggu family of puzzles. Each puzzle has p=6 pieces in alternating or rainbow colors. Some puzzles also have a black stand or frame which does not count as a piece.
Table 3: There are two types of commercially available Ziggu puzzles. Each piece either interacts with the previous and next pieces (2-neighbor) or the previous two and next two pieces (4-neighbor). This affects how many mazes that must be solved, the number of quaternary digits we use to represent an individual state, and the number of states in their shortest and longest solutions.
puzzles type pieces p mazes m digits n 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
Table 4: Integer sequences related to Ziggu puzzles encoded by n digits. Zigguhooked with p=6 dials is encoded by n=6 digits, so there are 46=4096 state encodings. Only 1093 of the encodings represent valid states, and 361 are on a shortest solution. In particular, 1322226 is an encoding, but it is not a valid state as the dials cannot be physically rotated as . So this encoding only contributes to the top row’s quantity as 132222𝕍6 and 132222𝕊6.
strings set cardinality growth values sequence
encoding n 4n θ(4n) 4,16,64,256,1024,𝟒𝟎𝟗𝟔, Oeis A000302
valid state 𝕍n (3n+11)/2 θ(3n) 4,13,40,121,364,𝟏𝟎𝟗𝟑, Oeis A003462
ziggu 𝕊n 62n3n5 θ(2n) 4,13,34,79,172,𝟑𝟔𝟏, 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 p=6 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 p=6 pieces, Zigguhooked will take longer to solve since it has 5 mazes, while Zigguflat has only 4 mazes.

Despite these seemingly large differences, all of the Ziggu puzzles are identical in an abstract sense. More specifically, all Ziggu puzzles with m 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 1 one peg to the left (right) for even (odd) n.

(a) Rotate dial 1 for 1011000¯=1011001.
(b) Rotate dial 5 for 101¯1000=1001000.
Figure 7: Spin-Out states are encoded as a binary string bnbn1b1 based on the orientation of its n dials. Horizontal translation is ignored, so (a) and (b) are both encoded as b7b6b1=1011000. The contours of the top and bottom rails ensure that at most two dials can be rotated in any given state. These include the rightmost dial b1 as in (a), and the dial to the left of the rightmost vertical dial as in (b). In particular, b1 or b2 can be changed from its start state b7b6b1=1111111, with the former change leading in the wrong direction (cf., Figure 2(b)).

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 m mazes and n=m+1 digits can have up to n2+1 non-reversing moves444For example, the state 2020202 (see Section 4) allows changes to each 2 and the last 0.,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.

(a) In 10203 the digit 0 is locked by the 2 digit to its right, but the other four digits can change. In the corresponding Zigguhooked state the green dial cannot rotate in either direction due to the orientation of the yellow dial, but all of the other dials can rotate in one direction.
(b) 2¯0203.
(c) 101¯03.
(d) 1021¯3.
(e) 10200¯.
Figure 8: In Ziggu puzzles there are states in which up to n2+1 digits can be changed. This is illustrated in (a) by a Zigguhooked state in which 52+1=4 of the n=5 dials can be changed. Digits that can change can only be changed in one way (i.e., incremented or decremented). These changes are shown in (b)–(e) with overlines and underlines indicating increments (counterclockwise rotation) and decrements (clockwise rotation), respectively. Note that (a) would not be encountered on the shortest solution since states on the shortest solution have at most three digits that can be changed (regardless of n). (An exception is the rightmost red digit, which can be freely incremented or decremented when it is not 0 or 3.)

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: 1,2,1,3,1,2,1,4,1, (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: 1,1,1,2,1,1,1,2,1,1,1,2,3,2,1,. Suppose that you always make the 1 moves in succession (i.e., when changing the first piece you do it three times in a row). Treating each 1,1,1 as a single 1 is natural in most of the Ziggu puzzles (e.g., the first dial in Zigguhooked moves freely between its two extreme states 0 and 3). With this assumption you just remember if your most recent move went up (e.g., 2 then 3) or down (e.g., 3 then 2). In the first case you will attempt to move up again (e.g., 2 then 3 then 4); if the puzzle does not allow this, then you will move down (e.g., 2 then 3 then 2). In the second case you move down again (e.g., 3 then 2 then 1) 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.

(a) Initial state.
(b) 13 valid locations.
(c) Maze exit.
Figure 9: (a) The standard Ziggu maze is on a 4-by-4 grid and its position is initially location (0,0). (b) There are 13 valid locations: (3,i) is invalid for 0i2. (c) The maze’s exit is accessible from location (3,3) (but only if higher mazes have been exited).

4.1 Ziggu Mazes

Definition 1.

A Ziggu maze is the 𝖲-shaped maze in Figure 9. It lives on a 4×4 grid, and the 13 valid positions are

{(i,j)i{0,1,2},j{0,1,2,3}}{(3,3)}. (3)

The rows and columns are labeled 0,1,2,3 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 (0,0), and the maze’s exit is accessible from (3,3).

An m-Ziggu maze is a sequence of m Ziggu mazes in states

(r1,c1),(r2,c2),,(rm,cm) where ri=ci+1 for all i=1,2,,m1. (4)

That is, the horizontal position of a maze is the vertical position of the maze to its right.

The base 4 representation of an m-Ziggu maze is the length n=m+1 base 4 string c1r1r2rm.

For example, the 3-Ziggu maze with state (0,1),(2,0),(3,2) has the maze representation . While there are 6 digits in the state description, since r1=c2 and r2=c3, we can describe the state of this 3-Ziggu maze with 4 digits with the base 4 string 1023.

Definition 1 immediately gives a bijection between the set of m-Ziggu mazes and the set of length m+1 base 4 strings in which 3 is the only digit appearing to the right of a 3.

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 p=6 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 m=p2=4 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 m=4 mazes, it requires n=m+1=5 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.

(a) Position 1203, of Zigguflat.
(b) Position 023, , of Zigguhooked.
Figure 10: The Ziggu states 1203 and 023 in Zigguflat and Zigguhooked, respectively. These are shorter versions of the puzzles with m=3 and m=2 mazes, respectively.

The Zigguhooked puzzle consists of p interlocking dials with p=6 in the commercially available version (see Figure 6(f).) Each maze is formed at the interface of two consecutive dials, so there are m=p1 mazes in total. The rotation of the dials are described by n=p 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 0, 1, 2, and 3 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 0213 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 f(n) and the number of states g(n) on a solution separately. These two quantities are closely related – the number of states is one more than the number of moves (i.e., g(n)=f(n)+1) – 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 56.

5.1 Gray Code Puzzle Solution Lengths

As was shown in Table 2, most Gray code puzzles run between states that are encoded as 0n and 10n1, or between 0n and 1n (or vice versa).

We previously saw the recurrences for the number of moves f(n) and the number of states g(n) 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 n discs is f(n)=2n. The number of moves on the shortest solution is g(n)=2n1. These quantities count the number of strings between 0n and 10n1 in the binary reflected Gray code.

Recurrences for the number of moves f(n) and the number of states g(n) 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 n dials is f(n)=2n+13. The number of moves on the shortest solution is g(n)=2n+13 These quantities count the number of strings between 0n and 1n in the binary reflected Gray code.

(a) The state space for n=3 discs. The shortest solution uses f(3)=5 moves.
(b) Puzzles with n discs require f(n)=f(n1)+2f(n2)+1 moves to solve with base cases f(1)=1 and f(2)=2.
Figure 11: Solving Spin-Out puzzles. An example state space is shown in (a) and the general recurrence for the number of moves required is in (b). Note that the first state is 1n (and not 0n) and this causes two complications: the initial move can be in the wrong direction and the sequence of dials turned 1,3,1,2,1, is not the ruler sequence. The same recurrence for the number of moves holds for other Gray Code puzzles between 0n and 1n (or vice versa) including The Brain.
Table 5: Formulae for the unique solution for two types of Gray code puzzles with n pieces. The base cases are for n=1,2.
puzzle quantity recurrence base cases closed form sequence
Towers of Hanoi moves 2f(n1)+1 1 2n1 Oeis A000225
Towers of Hanoi states 2g(n1) 2 2n Oeis A000079
Spin-Out moves f(n1)+2f(n2)+1 1,2 2n+1/3 Oeis A000975
Spin-Out states g(n1)+2g(n2)1 2,3 2n+1/3 Oeis A005578
Table 6: Formulae for the unique shortest and longest solutions to Ziggu puzzles with n digits. The base cases are for n=1,2. Note the similarity between the recurrence for the fewest moves (row 3) and 3f(n1)2f(n2), the recurrence for 2n.
quantity recurrence base cases closed form sequence
most moves 3f(n1)+3 3,12 (3n+13)/2 Oeis A029858 Oeis A123109
most states 3g(n1)+1 4,13 (3n+11)/2 Oeis A003462
fewest moves 3f(n1)2f(n2)+3 3,12 62n3n6 N/A
fewest states 3g(n1)2g(n2)+3 4,13 62n3n5 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.

(a) The top path between (0,0) and (3,0) is highlighted.
(b) The shortest solution uses the top path once.
(c) The longest solution uses the top path every time.
(d) Change the leftmost digit from 0 to 1.
(e) Change the leftmost digit from 1 to 2.
(f) Change the leftmost digit from 2 to 3.
Figure 12: Solving Ziggu puzzles requires moving back-and-forth through the m mazes. However, the top path shown in (a) only needs to be traversed once. More specifically, it is traversed only the first time in (b) the shortest solution, and every time in (c) the longest solution. To solve a Ziggu puzzle we must exit the last maze (i.e., maze m). This involves changing the leftmost digit from (d) 0 to 1, then (e) 1 to 2, and finally (f) 2 to 3. The shortest solution skips the back-and-forth traversal of the top path in maze m1 in both (e) and (f). Each omission skips over all but one of the states in maze m2 (not shown).
(a) Longest solution. Puzzles with n discs can be solved using f(n)=3f(n1)+3 moves. In these longest solutions no “shortcuts” are used. For example, all six optional vertices are used in Figure 4 for n=3.
(b) Shortest solution. Puzzles with n discs require f(n)=3f(n1)2f(n2)+3 moves. In these shortest solutions two “shortcuts” are used and correspond to the negative terms. For example, no optional vertices are used in Figure 4 for n=3.
Figure 13: Recurrences for the number of moves in the (a) longest and (b) shortest solutions to Zigguhooked. Base cases are f(1)=3 and f(2)=12 as the state spaces for n=1 and n=2 are paths (see Figure 4). The recurrences apply for other Ziggu puzzles with the same number of mazes.
Theorem 4.

The number of states in the shortest and longest solutions to Ziggu puzzles with n digits is f𝕊(n)=62n3n5 and f𝕍(n)=(3n+11)/2, respectively. These quantities count the number of Ziggu and valid strings between 0n and 3n in the quaternary Gray code, respectively. The number of moves is given by f(n)1.

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 n-bit binary strings and n-digit quaternary strings. Both Gray codes use reflection, which reverses a list of strings (but not the individual strings themselves). For example, 123,456,789R=789,456,123.

When ordering numbers or strings, Gray codes are not the same as standard numeric or lexicographic order. For example, in standard numeric order the 8-bit binary string 01111111 is followed by 10000000 (i.e., all n bits are flipped) whereas it is followed by 01111101 (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 2n binary strings of length n in which successive strings differ in only a single bit. The orders for n=2 and n=3 are provided below, where the overlines mark the bits that change to create the next string.

𝖡𝖱𝖦𝖢(2)=00¯,0¯1,11¯,10;𝖡𝖱𝖦𝖢(3)=000¯,00¯1,011¯,0¯10,110¯,11¯1,101¯,100. (5)

The global structure of the binary reflected Gray code is given by the following recursive formulae.

𝖡𝖱𝖦𝖢(1)=0,1;𝖡𝖱𝖦𝖢(n)=0𝖡𝖱𝖦𝖢(n1),1𝖡𝖱𝖦𝖢(n1)R (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 𝖡𝖱𝖦𝖢(3) are identical to 𝖡𝖱𝖦𝖢(2) except that 0 is prefixed to them. Similarly, the last four words of 𝖡𝖱𝖦𝖢(3) are identical to 𝖡𝖱𝖦𝖢(2) written in reflected order and with 1 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 0n (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 n=3 the list will begin 000,001,011,010. To create the next string we try complementing the bits in 010 from right-to-left: 010¯=011 is already in the list; 01¯0=000 is already in the list; 0¯10=110 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.

1=0,1,2,3;n=0n1,1n1R,2n1,3n1R for all n2. (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 n is built up from the sequences for smaller n.

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.

𝕍1=0,1,2,3;𝕍n=0𝕍n1,1𝕍n1R,2𝕍n1,3n for all n2. (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 3n1R, we append the state 3n, as there is no need to continue beyond the solved state 3n. See Figures 14 and 15.

(a) The states of Zigguflat with only the horizontal axis of a single maze.
(b) The states of Zigguflat with one maze.
Figure 14: This figure illustrates the recursive structure of both the longest and shortest solution to the Ziggu puzzles (they agree when n=1,2) using a version of Zigguflat with one maze. Figure 14(a) is the solution with only the horizontal part of the maze, which has 4 states. The successive rows in Figure 14(b) are created by prepending either a 0, 1, or 2 in each successive row to the state given by the column.
(a) The states of Zigguhooked with one dial.
(b) The states of Zigguhooked with two dials.
Figure 15: This figure illustrates the recursive structure of both the longest and shortest solution to a version of Zigguhooked with only 2 dials. Figure 15(a) is the solution with only 1 dial, which has 4 states. The successive rows in Figure 15(b) are created by prepending either a 0, 1, or 2 in each successive row to the state given by the column.

The recursive structure of the shortest solution to Ziggu puzzles is slightly more complicated. Let 𝕊1=core(1)=0,1,2,3, and for n2, let

𝕊n=0𝕊n1,core(n)[1:]andcore(n)=03n1,1core(n1)R,2core(n1),3n. (9)

The core(n)[1:] denotes all but the first element of core(n). This “list slicing” is needed because the word 03n1 appears as both the last word of 0𝕊n1 and the first word of core(n). 𝕊n is the sublist of 𝕍n obtained by omitting all states where 03 is followed by a digit that is not 3.

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 2k be an even base (binary is k=1, quaternary is k=2), and let w=wnw2w1 be a base-2k string of length n. Its reflected Gray code successor is obtained by incrementing or decrementing digit wi as follows

w={wnwi+1wi+1wi1w1if wi<2k1 and j=i+1nwj is evenwnwi1wi1wi1w1if wi>0 and j=i+1nwj is odd (10)

where i is the minimum index such that one of the two cases holds. If there is no such value of i, then w’s successor q is undefined.

For the quaternary (k=2) string w=012310, we see that i=2 since w2=1<3 and j=i+1nwj=3+2+1+0=6 is even. (10) gives that w=012320, so digit w2 was incremented.

If w is undefined for a quaternary string ww, then w=30n1, 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 4, no longer over an arbitrary even base 2k. Let q=qnq2q1𝕍n be a long quaternary string of length n. Its long successor is obtained by incrementing or decrementing digit qi as follows

𝗇𝖾𝗑𝗍𝕍(q)={qnqi+1qi+1qi1q1if j=i+1nqj is even and qi<3qnqi1qi1qi1q1if j=i+1nqj is odd, qi>0 and qi13 (11)

where i is the minimum index such that one of the two cases holds. If there is no such value of i, then q’s long successor 𝗇𝖾𝗑𝗍𝕍(q) is undefined.

For example, if q=20103, then i=1, since q1=3>0 and j=i+1nqj=0+1+0+2=3 is odd. Therefore 𝗇𝖾𝗑𝗍𝕍(q)=20102, as digit q1 was decremented.

Comparing (10) and (11), we see that the longest solution omits the strings in the quaternary reflected Gray code where a 3 is followed by digits that are not 3.

The successor rule for the shortest solutions adds an extra condition. Let q=qnq2q1𝕊n be a short quaternary string of length n. Its short successor is obtained by incrementing or decrementing digit qi as follows

𝗇𝖾𝗑𝗍𝕊(q)={qnqi+1qi+1qi1q1if j=i+1nqj is even, qi<3qnqi1qi1qi1q1if j=i+1nqj is odd, qi>0qi13qi1qi03 (12)

where i is the minimum index such that one of the two cases holds. If there is no such value of i, then q’s short successor 𝗇𝖾𝗑𝗍𝕊(q) is undefined.

For example, let q=20103. We see that now i1 since q2q1=03. Instead, i=3 since q3=1<3 and j=i+1n=0+2=2 is even. Therefore 𝗇𝖾𝗑𝗍𝕊(q)=20203, where we incremented digit q3.

Comparing (11) and (12), we see that the shortest solution omits the strings in the longest solution where a 03 is followed by digits that are not 3.

If 𝗇𝖾𝗑𝗍𝕍(q) or 𝗇𝖾𝗑𝗍𝕊(q) is undefined for q𝕍n or q𝕊n, then q=3n, 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 1n but the puzzle can have different valid states that also have first digit 1, 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 0n and the solved state 3n are the first and final valid states in both the longest and shortest solutions 𝕍n and 𝕊n, 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 w,v𝖡𝖱𝖦𝖢(n) with w=wnw2w1 and v=vnv2v1, we can determine if w occurs before v in the binary reflected Gray code as follows. Let wk be the leftmost digit where w and v differ, i.e, k is the index such that wi=vi for all k<in and wkvk. We compute that w occurs before v as follows.

w<v if {wk<vkif i=k+1nwi is evenwk>vkif i=k+1nwi is odd. (13)

For example, 101<100 in 𝖡𝖱𝖦𝖢(3), since k=1, w3+w2=1+0 is odd, and 1>0. The parity condition on the sum of the digits to the left of wk 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 w and v in the quaternary reflect Gray code n, as the parity condition still captures this same sublist reversal behavior. For example, 11013<11023 in 5 since k=2, w3+w4+w5=2 is even, and 1<2.

Since both the longest and shortest solutions are sublists of n, we see immediately that if we are given distinct states w,v𝕍n or w,v𝕊n, 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 O(1)-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 n-bit binary strings, all permutations of {1,2,,n}, 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 w=0n and starting index i=1. The array of direction d is initialized to have every entry be a 1, indicating that each digit should be increased (lines 2-4).

In every loop iteration, Algorithm 1 changes wi by the value d[i], the i-th right-to-left 1-indexed value in the array d (line 7). If this change results in wi being extremal, so wi equals 0 or 3, d[i] is set from increasing (1) to decreasing (1) or vice versa (line 9). In the next loop iteration, the digit to the left of wi will be changed (line 10). If wi was instead changed to a nonextremal value (1 or 2), then the next loop iteration will consider the digit to the right of wi, if wi is not the rightmost digit (lines 11-12). If wi is the rightmost digit, wi 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.

Algorithm 1 An algorithm for generating the shortest solution to the Ziggu puzzles. Each successive state is generated in worst-case O(1) time, making it a loopless algorithm. The algorithm uses one index variable, which requires O(log(n)) memory, and n bits of additional memory to store directions.

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., 2g+1 rows with g1). 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. n-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.