14
1
The 5-card magic trick involves a magician whose assistant gives them 4 shown cards and a hidden one, in this order, and the magician must guess the hidden one.
WARNING: Solution below! Leave now or get spoiled with it.
The solution
The trick here is that the five cards are given in a specific order!
\$c_1,...,c_5\$ are the 5 cards in the given order.
\$x_n\$ is the card number of \$c_n\$ in \$NO=[\text{A,2,3,4,5,6,7,8,9,T,J,Q,K}]\$ (number order).
\$a+b\$, where \$a\$ is a card number and \$b\$ is an integer, is equal to the card number \$b\$ steps to the right of \$a\$ in \$NO\$, wrapping to the beginning if necessary.
\$s_n\$ is the suit of \$c_n\$ in \$SO=[\clubsuit,\diamondsuit,\heartsuit,\spadesuit]\$ (suit order).
\$a\circ b\$, where \$a\$ is a card number and \$b\$ is a suit, denotes the card with card number \$a\$ and suit \$b\$.
\$a<b\$, where \$a\$ and \$b\$ are cards, is true if \$a\$'s suit is to the left of \$b\$'s suit in \$SO\$, or their suits are equal and \$a\$'s card number is to the left of \$b\$'s card number in \$NO\$.
\$a>b\$, where \$a\$ and \$b\$ are cards, is true if \$a<b\$ is false.
\$PI(a,b,c)\$, where \$a\$, \$b\$ and \$c\$ are cards, is the permutation index of this ordering of them, specified by the table below:
\$\begin{array}{|c|c|}\hline\text{Comparison}&PI(a,b,c)\\\hline a<b<c&1\\\hline a<b>c>a&2\\\hline a>b<c>a&3\\\hline a<b>c<a&4\\\hline a>b<c<a&5\\\hline a>b>c&6\\\hline\end{array}\$
The solution to the 5-card magic trick is problem is:$$c_5=(x_1+PI(c_2,c_3,c_4))\circ s_1$$
The challenge
So far, so good. However, doing the computation specified above is already asked for here. Instead, your challenge is, given the 5 cards in no specific order, to order them properly. This means that the first four cards in the output will represent the fifth. In other words, be the assistant. Requirements:
- \$s_5=s_1\$.
- \$x_5=x_1+PI(c_2,c_3,c_4)\$ (that is, this must be possible).
Example
Let's consider the set 7H,2D,6D,5C,6C. First of all, we take the 25 pairs:
7H,7H 7H,2D 7H,6D 7H,5C 7H,6C
2D,7H 2D,2D 2D,6D 2D,5C 2D,6C
6D,7H 6D,2D 6D,6D 6D,5C 6D,6C
5C,7H 5C,2D 5C,6D 5C,5C 5C,6C
6C,7H 6C,2D 6C,6D 6C,5C 6C,6C
Then, we obviously remove the 5 pairs that contain the same card twice, they don't exist in a single deck:
7H,2D 7H,6D 7H,5C 7H,6C
2D,7H 2D,6D 2D,5C 2D,6C
6D,7H 6D,2D 6D,5C 6D,6C
5C,7H 5C,2D 5C,6D 5C,6C
6C,7H 6C,2D 6C,6D 6C,5C
Afterwards, since the suits must be the same, different suits in a pair is a no-no:
2D,6D
6D,2D
5C,6C
6C,5C
Finally, we check if it's possible to get from the first card to the second by adding at most 6, removing half of the remaining pairs:
2D,6D
5C,6C
Now we have the valid pairs: 2D,6D and 5C,6C. The first card of each pair is card 1, while the last is card 5.
We're going to go with 5C,6C here for easiness. The whole set is 7H,2D,6D,5C,6C, so, removing the 2 cards in the pair we've chosen, we have 7H,2D,6D. These cards will represent 6 - 5 = 1, so we have to order them like "min, mid, max". 7H > 2D < 6D < 7H, or simply 2D < 6D < 7H, so we now have 2D,6D,7H.
The last step is to put all of this together, so our result will be 5C,2D,6D,7H,6C.
Clarifications
- You may use
10instead ofT. - You may use one of
♠♥♦♣,♤♡♢♧or♠♡♢♣instead ofCDHS, respectively. - This is code-golf, the shortest code wins.
Test cases
You can output one or more of the valid solutions included for each test case.
8S,TD,5C,QS,TS -> 8S,5C,QS,TD,TS
... 8S,TD,TS,5C,QS
... TS,5C,8S,TD,QS
JD,KH,4S,9D,8S -> 9D,KH,8S,4S,JD
... 4S,JD,KH,9D,8S
4H,4D,TH,KH,2C -> 4H,KH,4D,2C,TH
... TH,4D,2C,4H,KH
... KH,4D,TH,2C,4H
3S,KS,8S,KH,9H -> 9H,8S,KS,3S,KH
... 3S,KS,9H,KH,8S
... 8S,3S,9H,KH,KS
... KS,KH,9H,8S,3S
KH,TS,3C,7H,JD -> 7H,TS,JD,3C,KH
4C,KC,TD,JD,QS -> KC,JD,QS,TD,4C
... TD,4C,KC,QS,JD
AC,5H,8D,6D,8S -> 6D,AC,8S,5H,8D
AS,TC,3S,2H,9C -> 9C,2H,AS,3S,TC
... AS,9C,2H,TC,3S
4C,JS,AS,8H,JC -> JC,JS,AS,8H,4C
... JS,JC,4C,8H,AS
4H,QS,TH,QC,AC -> QC,4H,QS,TH,AC
... 4H,QS,QC,AC,TH
It might be easier to visualize the permutations by adding an Example column.
– Arnauld – 2018-11-07T14:14:25.420magic - A five card trick - How does it work? - Puzzling Stack Exchange – user202729 – 2018-11-07T16:12:05.193
How lenient is input? Are tuples of card number and house instead of length-2 strings acceptable? – Οurous – 2018-11-07T21:43:43.557
@Οurous That's not specified in the challenge; as long as it's reasonable (in your case, that seems reasonable enough), it's allowed. – Erik the Outgolfer – 2018-11-07T21:46:20.923