19
1
Write code to determine who wins a four-card trick in a game of Spades. Fewest bytes wins.
The input is a string that lists the four cards played in sequence like TH QC JH 2H
(Ten of Hearts, Queen of Clubs, Jack of Hearts, Two of Hearts). A card is given by two characters: a suit from CDHS
and a value from 23456789TJQKA
. You are guaranteed that the input is valid and the cards are distinct.
You should output a number 1, 2, 3, or 4 for the winner of the trick. In the example TH QC JH 2H
, the jack of hearts wins the trick, so you should output 3.
Your input and output must be exactly as described, except trailing newlines are optional.
Here are the Spades rules for winning a trick. The winning card is the highest card of the four, with some caveats. Spades is the trump suit, so any spade outranks any non-spade. The suit of the first card played is the lead suit, and only cards of that suit or spades are eligible to win the trick. Cards of the same suit are compared by their values, which are given in increasing order as 23456789TJQKA
.
Test cases:
TH QC JH 2H
3
KC 5S QS 9C
3
QD 2D TD 5D
1
9S 5D AD QS
4
3D 4C 3H JH
1
9S 4S TS JS
4
5H 9H 2C AD
2
5S 4C 3H QD
1
2H 2S KH AH
2
I give up, well played :P – orlp – 2015-04-20T11:14:34.803
I don't think the
.e
stuff is worth it - usingo
is 1 character shorter as I figure it. – isaacg – 2015-04-20T11:17:41.170@isaacg Your right. Funny thing is, I had the 27 solution before the
.e
28 solution. But the 27 solution ended with a)
and therefore also had 28 bytes. :oops: – Jakube – 2015-04-20T11:22:56.6101I thought of a way to save another 2 characters: Translate from
+@z1"KTA"
to"KTAZ"
, but instead of using the strings directly useJ"KTAZ"
at the beginning and+@z1J
toJ
. – isaacg – 2015-04-20T12:35:22.000@isaacg Very clever. Thanks. Btw. I thinking quite a while about making the 3rd argument of
X
optional (Only if a and b are strings). But I'm not really sure, ifXab)
should evaluate toXab_b
(inverted b, would be nice for stuff likeXa"</\>"
) orXab+tbhb
(b shifted). What your preference? – Jakube – 2015-04-20T12:53:35.133Hm. Well, since I was thinking of adding a rotate function to Pyth, probably
.r
, I'll use that for the latter behavior and the reversal forX
with the third argument omitted. Both will have a 2 character overhead. – isaacg – 2015-04-20T12:55:28.743