Background
Inspired by this video by Matt Parker
A Faro shuffle is a perfect riffle shuffle where the deck is exactly interleaved with its other half. This is how to perform one:
- the deck of 52 cards is split into two piles of 26 cards each
- the two piles are interleaved. In the case of an "in" shuffle, the bottom card of the top pile ends up on the bottom, and the top card of the bottom pile ends up on the top (the outer cards move inwards). In the case of an "out" shuffle, the top card of the top pile ends up on top and vice versa (the outer cards stay on the outside).
For example, with a demonstration deck of only 6 cards, the steps are as follows:
- Start:
[1, 2, 3, 4, 5, 6]
- Split:
[1, 2, 3] [4, 5, 6]
- For an "in" shuffle, interleave like this:
[4, 1, 5, 2, 6, 3]
(zip the second half with the first half) - For an "out" shuffle, interleave like this:
[1, 4, 2, 5, 3, 6]
(zip the first half with the second half)
Challenge
Given a list of values representing "in" and "out" Faro Shuffles, determine the minimum number of times that list needs to repeat such that a deck of 52 distinct cards will return to its starting arrangement. Note that this is not the same value shown in Matt's video; it is divided by the length of the input.
If the deck returns to the starting arrangement part way through the list (for example, [0, 0, 0, 0, 0, 0]
(6 out shuffles) would finish after 1 full set of 6 and then 2 more shuffles), it doesn't count as having finished the cycle because it wasn't completed at the end of the list (so the output for that example would be LCM(6, 8) / 6 = 4
).
Example
Input: [0, 1, 1, 0, 1, 1, 1]
(using 0
for "out" shuffles and 1
for "in" shuffles, and representing the starting deck is [0, 1, 2, 3, ... 48, 49, 50, 51]
)
- First, we perform the shuffles as they are specified in the input: out, in, in, out, in, in, in. This gives the output:
[0, 26, 1, 27, 2, 28, 3, 29, 4, 30, 5, 31, 6, 32, 7, 33, 8, 34, 9, 35, 10, 36, 11, 37, 12, 38, 13, 39, 14, 40, 15, 41, 16, 42, 17, 43, 18, 44, 19, 45, 20, 46, 21, 47, 22, 48, 23, 49, 24, 50, 25, 51]
- Now, we check if that has caused the list to return to its exact original arrangement
- Repeat these steps, and output the number of times we've looped. In this case, it takes 1260 repetitions of
[0, 1, 1, 0, 1, 1, 1]
, so our output is 1260
Test cases
Using 0
for "out" shuffles and 1
for "in" shuffles.
[0] 8
[1] 52
[0, 1] 252
[0, 0, 1] 20
[0, 1, 0] 20
[1, 0, 0] 20
[0, 1, 1, 0, 1, 1, 1] 1260
[0, 0, 0, 0, 0, 0, 0, 1] 2
[0, 0, 0, 0, 0, 0] 4
[0, 0, 0, 0, 0, 0, 0, 0, 0] 8
[0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1] 144
[1, 0, 1, 1, 1, 0, 1, 1, 1] 60
[0, 1, 1, 0] 44
[1, 1, 1, 1, 0, 1, 0, 1, 0] 306
[1, 1, 0, 1, 1, 0, 0, 1, 1, 0] 210
[1, 0, 0, 0, 0, 0, 0, 1] 2
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1] 168
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1] 228
[1, 0, 1, 0, 1] 60
[1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1] 66
[0, 0, 0, 0, 1, 0] 252
Rules
- The values that represent "in" and "out" in the input list can be any two distinct values, within reason. For example,
0
and1
is allowed, butcode_to_do_an_in_shuffle()
andcode_to_do_an_out_shuffle()
are not - Standard loopholes are forbidden
- You may use any sensible I/O method
- This is code-golf, so the shortest code in bytes wins