Solution to Matt Parker's Math Puzzle #4: Card Puzzle
How many cards do you have to flip without looking to get four arbitrarily turned playing cards all facing down.
Matt Parker, the creator of The Parker Square, regularly poses online math puzzles. Below is the video in which he explains the problem I’m trying to solve.
Can you construct such a sequence of flipping cards that if given four arbitrarily turned cards there will be a point in the sequence where all four cards are facing down?
Firstly we can see that each sequence of flipping cards is it’s own inverse. Which means that we can think of the problem as inverse. Instead of translating each card configuration into face down cards, we can start with all face down cards and translate it into every configuration.
That automatically means that any sequence applied twice will be an identity and will not change any of the cards, i.e. every sequence is it's own inverse.
$\underline{\implies}$
for any $\mathbf{C} \in S^4$ we have $H^{(m)}=\colon H_m \circ H_{m-1} \circ ... \circ H_2 \circ H_1$ $$ H^{(m)}(\mathbf{C}) = \Big(🂠, 🂠, 🂠, 🂠\Big) $$ apply $H^{(m)}$ on both sides $$ \not H^{(m)}(\not H^{(m)}(\mathbf{C})) = H^{(m)}\Big(🂠, 🂠, 🂠, 🂠\Big) \\ \mathbf{C} = H^{(m)}\Big(🂠, 🂠, 🂠, 🂠\Big) $$
$\underline{\impliedby}$
for any $\mathbf{C} \in S^4$ we have $H^{(m)}=\colon H_m \circ H_{m-1} \circ ... \circ H_2 \circ H_1$ $$ H^{(m)}\Big(🂠, 🂠, 🂠, 🂠\Big) = \mathbf{C} $$ apply $H^{(m)}$ on both sides $$ \not H^{(m)}\left(\not H^{(m)}\Big(🂠, 🂠, 🂠, 🂠\Big)\right) = H^{(m)}(\mathbf{C}) \\ \Big(🂠, 🂠, 🂠, 🂠\Big) = H^{(m)}(\mathbf{C}) $$ □
We can visualize the problem as graph traversal where each of the nodes represents a specific configuration of cards and edges represent single card flips. This is equivalent to a four dimensional hypercube.
Below is a tool I built to make this easier to understand. We can change the number of cards in the game to change the dimensionality of the hypercube graph. We can traverse it flipping cards or by clicking on nodes to load that state.
The thicker lines between cubes represent all the connections between nodes in the same corner to prevent overcrowding the graph with edges.
- Click cards to flip them.
- Click -/+ to remove or add cards.
- Click nodes below to jump to card state.
The problem as we now understand it can be posed as “How many steps does it take to traverse all corners of a 4D hypercube?” We know that we have to visit $2^4=16$ corners, which will take at minimum $15$ steps. Can we visit all of them without repeating any corners?
A path over a graph that visits all vertexes exactly once is called a Hamiltonian path. Does a 4D hypercube have a Hamiltonian path? The answer is a resounding yes. Not only can we traverse all vertexes in 15 steps, we can end our journey adjacent to our starting point, i.e. the graph has a Hamiltonian cycle.
Below is an example of such a path. Horizontal lines ⸺ equate to flipping the first card, vertical lines | represent flipping the second card, diagonals \ flip the third, and jumping between the two cubes means flipping the fourth card.
All the arguments we made above are not limited to four cards or four dimensions. In fact, if we have $N$ cards we can solve the problem by traversing the Hamilton path in a $N$-dimensional hypercube which we can do in $2^N-1$ steps.
We can construct an optimal solution recursively. Let’s denote $S_N$ as an optimal sequence of flips for $N$ cards.
End condition
For one card it’s easy. We only have one card and therefore only one winning move.
Recursive step
For any number of cards $N>1$ we can first traverse all possibilities for the first $N-1$ cards, flip the $N$-th card, and again repeat the sequence for the first $N-1$ cards.
Analysis
I want to look at the size of each solution to confirm that they are indeed the minimum size.
□
Number of cards $N$ | Optimal flipping sequence $S_N$ | Length of the sequence $|S_N| = 2^N-1$ |
---|---|---|
1 | 1 | 1 |
2 | 1, 2, 1 | 3 |
3 | 1, 2, 1, 3, 1, 2, 1 | 7 |
4 | 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1 | 15 |
5 | 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1 | 31 |
6 | 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 6, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2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