For the purposes of this question, a deck of cards is formatted in this way:
[
"AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS",
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD",
"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",
"J", "J"
]
Cards are always formatted as value, followed by suits. E.g. AS is the Ace of Spades. The two single J's are Jokers. We want to shuffle this deck of cards, but the shuffle must be SuperbTM.
A Superb ShuffleTM is one in which:
- No two cards (except Jokers) of the same suit are adjacent.
- No card (except Jokers) is adjacent to one of the same value.
- No card (except Jokers) is adjacent to one of an adjacent value (one higher or one lower in this order, A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A. Notice that Ace can not be adjacent to either a 2 or a King).
- The Jokers can be in any position.
- The definition of a Superb ShuffleTM does not require the cards to be in a different order each time they are shuffled. Which isn't very superb, but it is SuperbTM.
Because that's SuperbTM.
An example might be:
[
"AS", "5D", "9H", "KC", "2D", "6H", "10C", "QS", "3H", "7C", "9S",
"KD", "4C", "6S", "10D", "AC", "3S", "7D", "JH", "J", "4D", "8H",
"QC", "AD", "5H", "9C", "JS", "2H", "6C", "8S", "QD", "3C", "5S",
"9D", "KH", "2S", "6D", "10H", "J", "3D", "7H", "JC", "KS", "4H",
"8C", "10S", "AH", "5C", "7S", "JD", "2C", "4S", "8D", "QH"
]
The challenge:
- Write some code to execute a superb shuffle
- Use any language.
- The input can be either:
- a deck of cards as described above in the same order, as an array or other list structure.
- No input (the code generates a deck of cards in that order)
- The output must be a full deck of cards in a Superb ShuffleTM as described above.
- Attempt to perform your Superb ShuffleTM in the smallest number of bytes.
- A link to an online interpreter such as Try It Online is preferred, but optional.
Happy shuffling!
13 Answers 13
Ruby, (削除) 28 (削除ここまで) 26 bytes
->x{x.map{x[($.+=17)%54]}}
Today I decided to have a look at this problem again, and I could shave (削除) 3 (削除ここまで) 5 bytes from the previous solution (reported below).
Ruby, 31 bytes
->x{(0..53).map{|r|x[r*17%54]}}
Explanation:
I'm picking one card, then skipping over the next 16 and start from the first card when I reach the last card of the deck. 17 and 54 are mutually prime, so I'm sure to pick all cards.
The 17th position is guaranteed to be a different suit and the difference in value is at least 2: the 13th (or 15th) card has the same value and a different suit, so by skipping other 4 (or 2), the value is right.
-
6\$\begingroup\$ Well done on finding the process every other answer uses ;) \$\endgroup\$AJFaraday– AJFaraday2018年06月18日 12:12:19 +00:00Commented Jun 18, 2018 at 12:12
-
3\$\begingroup\$ Can you explain how you found the *17%54? Just trial and error or is there some obvious maths that I'm missing? \$\endgroup\$Daniel– Daniel2018年06月18日 12:36:37 +00:00Commented Jun 18, 2018 at 12:36
-
\$\begingroup\$ @Daniel 17 is the required minimum distance between two differently suited cards that are not adjacent numerically (accounting for the two jokers; e.g. 17 steps gets you from the Ace of Clubs to the 3 of Spades); 54 is the number of cards in the deck. \$\endgroup\$Hellion– Hellion2018年06月18日 20:19:57 +00:00Commented Jun 18, 2018 at 20:19
Japt, (削除) 6 (削除ここまで) (削除) 5 (削除ここまで) 4 bytes
Splits the input array into sub-arrays of every 16th element and flattens.
óG c
Java 10, (削除) 72 (削除ここまで) 65 bytes
d->{var r=d.clone();for(int i=54;i-->0;r[i*7%54]=d[i]);return r;}
Similar as @GB's Ruby answer, but by using i*7%54 on the result-array, instead of i*17%54 on the input-array to save a byte.
Explanation:
d->{ // Method with String-array as both parameter and return-type
var r=d.clone();// Result-String, starting as a copy of the input
for(int i=54;i-->0;
// Loop `i` in the range (54, 0]
r[ // Set an item in the result-array at index:
i*7%54 // Index `i` multiplied by 7, and then take modulo-54
]=d[i]); // To the `i`'th item in the input-Deck
return r;} // Return the result-Array
-
\$\begingroup\$ Unfortunately, the result contains numerous cards which are adjacent to cards of the same suit. It starts with
AS, 6S, JS, 3D, 8D, KD,. \$\endgroup\$AJFaraday– AJFaraday2018年06月18日 11:50:55 +00:00Commented Jun 18, 2018 at 11:50 -
\$\begingroup\$ @AJFaraday TIO was still with 11 instead of 7. Could you check it again. Perhaps I missed something else, but I think it should be correct now (I hope). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2018年06月18日 11:53:52 +00:00Commented Jun 18, 2018 at 11:53
-
\$\begingroup\$ That's got it now. Nice work! \$\endgroup\$AJFaraday– AJFaraday2018年06月18日 11:56:02 +00:00Commented Jun 18, 2018 at 11:56
JavaScript, 35 bytes
x=>x.map((a,i)=>i%2?a:x[(i+20)%54])
Taking an array of deck as input, and replacing each odd value with another card that is "20 cards" away on the deck.
-
1\$\begingroup\$ Aha, when I said "everyone" in my post was incorrect; this is how I went too! \$\endgroup\$Jonathan Allan– Jonathan Allan2018年06月18日 17:43:56 +00:00Commented Jun 18, 2018 at 17:43
JavaScript, 27
Another one based off of the ruby answer
d=>d.map((_,i)=>d[i*17%54])
Edited in an obvious shortening
Perl 6, (削除) 21 20 (削除ここまで) 18 bytes
Thanks to Brad Gilbert b2gills for -2 bytes
{.[$++*17%$_]xx$_}
Yet another port of G B's answer. (削除) Note that, while the global variable However, $! is not reset between functions, the value doesn't matter, since any order of the output is valid. (削除ここまで)$ is reset.
Explanation:
{ } #Anonymous code block
xx$_ #Repeat size of inputted array (54) times
.[ ] #Get from the inputted array, the value at index
$++*17%$_ #The incremented variable, multiplied by 17, modded by size of the array
-
1\$\begingroup\$ This works just as well with an unnamed state var
$as it does with$!or$/. Also if you used$_instead of@_you could start it with.[...]instead of@_[...]saving another byte. \$\endgroup\$Brad Gilbert b2gills– Brad Gilbert b2gills2018年06月18日 14:20:01 +00:00Commented Jun 18, 2018 at 14:20
05AB1E, (削除) 9 (削除ここまで) (削除) 7 (削除ここまで) 5 bytes
ā17*è
Port of @GB's Ruby answer, so make sure to upvote him!
-2 bytes by printing each card with a new-line delimiter instead of wrapping it to a result-list
-2 bytes thanks to @Mr.Xcoder
Explanation:
ā # 1-indexed length range [1 ... length_of_input_list (54)]
17* # `k`: Multiply each index by 17
è # And then replace each item with the 0-indexed `k`'th card of the input-list
# (with automatic wrap-around)
-
1\$\begingroup\$
ā17*èshould save 2 more bytes \$\endgroup\$Mr. Xcoder– Mr. Xcoder2018年06月18日 12:56:01 +00:00Commented Jun 18, 2018 at 12:56
T-SQL, 31 bytes
SELECT c FROM t ORDER BY i*7%54
If you don't care about an extra column in the output I can get it down to 29 bytes:
SELECT*FROM t ORDER BY i*7%54
So you can verify my output is "Superb", here is the deck it produces:
J, 5H, 8S, KH, 3D, 8C, JD, AS, 6H, 9S, AC, 4D, 9C, QD,
2S, 7H, 10S, 2C, 5D, 10C, KD, 3S, 8H, JS, 3C, 6D, JC,
AH, 4S, 9H, QS, 4C, 7D, QC, 2H, 5S, 10H, KS, 5C, 8D,
KC, 3H, 6S, JH, AD, 6C, 9D, J, 4H, 7S, QH, 2D, 7C, 10D
(Generated using the new SQL 2017 addition, STRING_AGG):
SELECT STRING_AGG(c,', ')WITHIN GROUP(ORDER BY i*7%54)FROM t
The hard part for me was not the select code, it was populating the input table (which is allowed for SQL per our IO rules).
Because SQL is inherently unordered (it only guarantees a certain order if you include an explicit ORDER BY clause), I had to include that original order as a field i in the input table t. This also means I can use it to sort, using the same "relatively prime" factor/mod process everyone else is using. I found that i*7%54 worked just as well as i*17%54.
Here are the commands to set up and populate the input table t, based on my solution to this related question:
CREATE TABLE t (i INT IDENTITY(1,1), c VARCHAR(5))
--Insert 52 suited cards
INSERT t(c)
SELECT v.value+s.a as c
FROM STRING_SPLIT('A-2-3-4-5-6-7-8-9-10-J-Q-K','-')v,
(VALUES('S',1),('D',2),('H',3),('C',4))s(a,b)
ORDER BY s.b
--Insert Jokers
INSERT t(c) SELECT 'J'
INSERT t(c) SELECT 'J'
-
\$\begingroup\$ Would the
inot be considered additional input here? \$\endgroup\$Shaggy– Shaggy2018年06月18日 19:59:33 +00:00Commented Jun 18, 2018 at 19:59 -
\$\begingroup\$ @Shaggy The question says I can get the input deck in the original (listed) order. The only way to guarantee this in SQL is to have the order be an explicit part of the input, because SQL tables have no "default order". So, I view it as a necessary component of the input. But don't worry, SQL is rarely competitive anyway :) \$\endgroup\$BradC– BradC2018年06月18日 20:35:35 +00:00Commented Jun 18, 2018 at 20:35
Jelly, (削除) 5 (削除ここまで) 4 bytes
s4ZẎ
Turns out the way (削除) everyone else (削除ここまで) everyone else except The random guy is doing it saves a byte :(
Credit to G B for their method.
The way I went...
ṙÐe20
How?
Fix every other card and intersperse it with a rotation of the deck left by 20 places (18 and 22 places also work; furthermore so does either direction of rotation as well as fixing either odd or even cards)
ṙÐe20 - Link: list of the card strings (lists of characters)
20 - place a literal twenty on the right
Ðe - apply to even indices:
ṙ - rotate left (by 20)
That is (using T for 10 and rj & bj for the Js):
input: AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D 8D 9D TD JD QD KD AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC rj bj
ṙ20: 8D 9D TD JD QD KD AH 2H 3H 4H 5H 6H 7H 8H 9H TH JH QH KH AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC rj bj AS 2S 3S 4S 5S 6S 7S 8S 9S TS JS QS KS AD 2D 3D 4D 5D 6D 7D
ṙÐe20: AS 9D 3S JD 5S KD 7S 2H 9S 4H JS 6H KS 8H 2D TH 4D QH 6D AC 8D 3C TD 5C QD 7C AH 9C 3H JC 5H KC 7H bj 9H 2S JH 4S KH 6S 2C 8S 4C TS 6C QS 8C AD TC 3D QC 5D rj 7D
PowerShell 3.0, (削除) 30 (削除ここまで) 26 Bytes
$args[(0..53|%{$_*17%54})]
-4 thanks to Mazzy
Old code at 30 bytes
param($d)0..53|%{$d[$_*17%54]}
Another port of G B's method.
-
\$\begingroup\$ 26 bytes
$args[(0..53|%{$_*17%54})]. \$\endgroup\$mazzy– mazzy2018年07月09日 17:52:49 +00:00Commented Jul 9, 2018 at 17:52 -
\$\begingroup\$ @Mazzy I feel like that breaks input specs. Sure they're collected into $args but you're not actually passing one in. \$\endgroup\$Veskah– Veskah2018年07月09日 21:43:30 +00:00Commented Jul 9, 2018 at 21:43
-
-
\$\begingroup\$ in additional, it seems to me that there is no need to count the characters
&{and}. You can to saveparam($d)0..53|%{$d[$_*17%54]}to a file. and call this file without&{...}\$\endgroup\$mazzy– mazzy2018年07月09日 22:37:37 +00:00Commented Jul 9, 2018 at 22:37 -
1\$\begingroup\$ @mazzy Yeah, I've always been a bit unsure of which control parts to keep so just usually defaulted to making it a script block. I'll strip it in the future though. \$\endgroup\$Veskah– Veskah2018年07月09日 22:44:30 +00:00Commented Jul 9, 2018 at 22:44
Charcoal, 8 bytes
×ばつ17κ
Try it online! Link is to verbose version of code. Another port of @GB's Ruby answer. Explanation:
θ Input array
E Map over elements
κ Current index
17 Literal 17
×ばつ Multiply
θ Input array
§ Cyclically index
Implicitly print each result on its own line
Red, 44 bytes
func[a][append/dup a a 16 prin extract a 17]
Another interpetation of G B's code. I append 16 copies of the deck to itself and then extract each 17th card.
Tinstead of10? \$\endgroup\$