| Bytes | Lang | Time | Link |
|---|---|---|---|
| 026 | Japt | 180727T162938Z | Shaggy |
| 028 | 05AB1E | 241219T112825Z | Kevin Cr |
| 088 | JavaScript ES6 | 180726T201309Z | Arnauld |
| 062 | Zsh | 241218T164712Z | pxeger |
| 087 | Perl 5 ap | 241218T153439Z | Xcali |
| 080 | JavaScript Node.js | 241218T021248Z | l4m2 |
| 120 | Wolfram Language Mathematica | 241218T064128Z | Introduc |
| 156 | Java 10 | 180727T073824Z | Kevin Cr |
| 091 | R | 180726T200832Z | ngm |
| 209 | Pascal FPC | 180901T162258Z | AlexRace |
| 226 | Kotlin | 180901T045149Z | JohnWell |
| 264 | PHP | 180730T113913Z | Sam Dean |
| 095 | Python 2 | 180726T191624Z | lynn |
| 106 | JavaScript ES6 | 180728T073349Z | redundan |
| 031 | Brachylog | 180727T114033Z | Sundar R |
| 018 | Jelly | 180726T233523Z | Erik the |
| 135 | Clean | 180727T032136Z | Οurous |
| 066 | Retina 0.8.2 | 180726T213555Z | Neil |
Japt, 37 26 bytes
Takes input as a 2D array of strings, using T for 10.
"A{9õ2 ¬}TJQKA"øUüÌcÈmάá5
"A{9õ2 ¬}TJQKA"øUüÌcÈmάá5 :Implicit input of array U
"A :String literal
{ : Interpolate
9õ2 : Range [2,9]
¬ : Join
} : End interpolate
TJQKA" :End string literal
ø :Contains any element of
Uü : Sort & group U by
Ì : Last element
c : Flat map
È : Through the following function
m : Map
Î : First element
¬ : Join
á5 : Permutations of length 5
05AB1E, 28 bytes
5ãʒ€θË}ε€нJ9YŸJ.•B²ä•uìĆså}à
Input as a list. Uses T for 10.
Outputs 1 for truthy and 0 or an empty string for falsey.
Try it online or verify all test cases.
Explanation:
5ã # Cartesian product of 5, to get all possible quintuplets
# (including duplicated items, but that's fine for now)
ʒ # Filter this list of quintuplets by :
€θ # Where every last item of the pair (the suit)
Ë # Are all equal
}ε # After the filter: map the remaining quintuplets by:
€н # Where every first item of the pair (the values)
J # Joined together to a string
så # Is a substring of
9YŸJ.•B²ä•uìĆ # String "AKQJT98765432A":
9YŸ # Push a list in the range [9,2]
J # Join it together to a string
.•B²ä• # Push compressed string "akqjt"
u # Convert it to uppercase
ì # Prepend it in front of the "98765432"
Ć # Enclose; append its own head (the "A")
}à # After the map: maximum to check whether any is truthy
# (which is output implicitly as result)
JavaScript (ES6), 88 bytes
-10 thanks to @l4m2
a=>a.some(m=([t,c,C])=>(g=k=>k&&1+g(k&k/2))(m[C||c]|=8193>>"A234567891JQK".search(t))>4)
How?
For each suit that appears at least once in the input, we convert all cards \$c\$ of this suit into a 14-bit bitmask \$m\$ where the bit #13 (Ace) is copied to bit #0 to handle the steel wheel (A,2,3,4,5) and count the number of consecutive bits. If it's greater than 4, we have a straight flush.
Zsh, 62 bytes
r=(A {2..10} J Q K A)
eval>$@ ! '||ls ${^r:'{0..9}:5}{H,S,C,D}
Outputs via exit code: 2 for no, 0 for yes.
Explanation:
r=(A {2..10} J Q K A): set up the array of ranks>$@: create a file for each item in the inputeval: construct and evaluate the following program:!: false{H,S,C,D}: for each suit,{0..9}: for each index of starting rank,||: the previous loop iteration succeeded, or:${r::5}: get five elements ofrstarting at that index${^}: append the suit to each of those elementsls: try to find files with those names
Perl 5 -ap, 87 bytes
$_=join$/,map{//;join'',map$_.$',A,2..9,T,J,Q,K,A}H,C,D,S;for$b(@F){s/$b/ /g}$_=/ /
JavaScript (Node.js), 80 bytes
a=>/0{5}/.test(a.map(s=>"A23456789TJQKA".replace(/./g,c=>a.indexOf(c+s[1])>>9)))
JavaScript (Node.js), 91 bytes
a=>a.some(s=>(g=n=>n--?~a.indexOf(S[n+S.search(s[0])]+s[1])&&g(n):1)(5))
S="A23456789TJQKA"
Wolfram Language (Mathematica), 120 bytes
f[c_]:=Or@@(ContainsAll[c,#]&/@Flatten[Partition[#,5,1]&/@Outer[(#2<>#1&),##]&@@Characters@{"HCSD","A234567891JQKA"},1])
Java 10, 189 167 165 164 160 157 156 bytes
s->{int i=10;for(;i-->0;)i=s.matches("AKQJT98765432A".substring(i,i+5).replaceAll(".","(?=.*$0\\\\1)").replaceFirst(".1","([HSDC])")+".*")?-2:i;return-1>i;}
Takes the input as a single space-delimited String
(i.e. "AS 2S 3S 4S 5S").
-22 bytes thanks to @OlivierGrégoire.
-1 byte thanks to @AlexRacer.
Golfed version of the code that I've used for Project Euler #54, which I primarily did with regexes (for fun and to learn more about regexes). Without regexes it would probably have been better for performance and easier (probably also applies for golfing this answer; will take a look later on).
Explanation:
s->{ // Method with String parameter and boolean return-type
int i=10;for(;i-->0;) // Loop `i` in the range (10,0]:
i=s.matches( // If the input matches the following regex:
"AKQJT98765432A".substring(i,i+5)
.replaceAll(".","(?=.*$0\\\\1)")
.replaceFirst(".1","([HSDC])")
// Five adjacent cards
+".*")? // With optionally zero or more other characters
-2 // Set `i` to -2, which also stops the loops at the same time
:i; // Else: leave `i` unchanged to continue
return-1>i;} // Return whether `i` is not -2 (so whether the loop has finished)
Additional regex explanation:
"AKQJT98765432A".substring(i,i+5)takes five adjacent cards based oni.replaceAll(".","(?=.*$0\\\\1)")replaces each of those cards with"(?=.*c\\1)"(wherecis the card-character).replaceFirst(".1","([HSDC])")will then replace the first\\1with([HSDC]).
I.e. the total regex to check the Straight Flush for cards in the value-range [9,5] will become:
^(?=.*9([HSDC]))(?=.*8\\1)(?=.*7\\1)(?=.*6\\1)(?=.*5\\1).*$
(NOTE: String#matches implicitly adds the trailing/leading ^...$ to check the entire String.) This regex will:
^(?=.*9([HSDC]))(?=.*8\\1)(?=.*7\\1)(?=.*6\\1)(?=.*5\\1).*$
^ $ Match the entire string
(?= )(?= )(?= )(?= )(?= ) Do positive lookaheads to check
each card
.* .* .* .* .* With optional leading characters
in front of every card
.* And any trailing characters at
the end of the entire hand
9 8 7 6 5 The five adjacent values
[HSDC] With a suit
( ) \\1 \\1 \\1 \\1 which is the same for all cards
R, 128 126 94 91 bytes
function(x,r=rle(outer(y<-chartr("J-X","A2-9TJQKAS",LETTERS),y,paste0)%in%x))any(r$l>4&r$v)
Original logic shortened considerably by @J.Doe.
Makes a 26 by 26 matrix with mostly nonsense but all the cards (with the Aces repeated at the bottom) contained in rows 10 to 23 of columns 3,4,8 and 24. The matrix is created by concatenating all combinations of the upper case alphabet with letters J through X replaced by A,2-9,T,J,Q,K,A,S via chartr. We get C, D, H for free!
The %in% flattens the matrix column-wise into a vector. Then see if run-length encoding is greater than 4 for any run of TRUE matches.
Pascal (FPC), 223 216 210 209 bytes
var a,b:char;c:set of byte;i:byte;begin repeat readln(a,b);i:=pos(b,'HDC')*14+pos(a,'23456789TJQK');c:=c+[i];if a='A'then c:=c+[i+13]until eof;i:=0;while not([i..i+4]<=c)or(i mod 14>9)do i:=i+1;write(i<52)end.
Uses T for 10. Input contains 1 card per line.
Now I golfed it so much that I don't know how it works anymore...
Explanation:
var a,b:char; //for reading cards
c:set of byte; //this set is for remembering which cards are present in the input
//14 numbers used for each suit
i:byte;
begin
repeat
readln(a,b); //read rank into a, suit into b and a newline
i:=pos(b,'HDC')*14+pos(a,'23456789TJQK');
//temporary use i to calculate corresponding number for the card
//pos() gives 0 if b is not found
//1st pos() is for the group of numbers for that suit, 2nd pos() is for offset
c:=c+[i]; //include i into set
if a='A'then c:=c+[i+13] //if rank is A, include the number at the end of group as well
until eof;
i:=0;
while not(
([i..i+4]<=c) //if NOT 5 cards in a row are present...
and //while the check is started from 10 (T)...
(i mod 14<10) //(otherwise, it is checking across 2 different suits)
)do i:=i+1; //increment i, otherwise stop
write(i<52) //if i<=51, there is a straight flush starting at the card corresponding to i
//(if there isn't a straight flush, i stops at 252 due to i..i+4, I don't know why)
end.
Kotlin, 226 bytes
Used T for 10 so all cards are 2 characters in length.
{h:List<String>->val m=List(4){mutableSetOf<Int>()}
for(c in h)m["CDHS".indexOf(c[1])].add("A23456789TJQK".indexOf(c[0]))
var r=0>1
for(b in m){if(b.contains(0))b.add(13)
for(i in 0..9)r=b.containsAll((i..i+4).toList())||r}
r}
PHP, 264 bytes
It echos 1 if it is a straight flush and 0 or null if not.
If you name the file 1X then you can save 11 bytes as you don't need to change $argv[0]. Not sure at the moment why the filename can break it.
For some reason the strings :;<=> get sorted before the strings 0123456789 by asort in TIO even though :;<=> have ASCII values 58-62 and 0123456789 have ASCII values 48-57. So if you take the code from the TIO link or below and use PHPTester with the following test suite it works.
$argb[0] = [".code.tio", "AS", "2S", "3S", "4S", "5S"]; // => true
$argb[1] = [".code.tio", "3D", "9C", "4S", "KH", "AD", "AC"]; // => false
$argb[2] = [".code.tio", "5D", "6D", "7D", "8H", "9D", "TD", "JD"]; // => false
$argb[3] = [".code.tio", "JC", "7C", "5D", "8C", "AC", "TC", "9C", "5S"]; // => true
$argb[4] = [".code.tio", ]; // => false
$argb[5] = [".code.tio", "AS", "2S", "3S"]; // => false
$argb[6] = [".code.tio", "JC", "QC", "KC", "AC", "2C"]; // => false
$argb[7] = [".code.tio", "TC", "JC", "QC", "KC", "AC", "2C"]; // => true
$argb[8] = [".code.tio", "2H", "3H", "4H", "5H", "6H", "7H"]; // => true
for ($z=0; $z<9;$z++){
$argv=$argb[$z];
array_shift($argv);
unset($a,$b,$c,$d,$e,$f,$g,$h,$i);
$f=false; // not needed, just removes several notices
// TIO code here
echo "<br>";
TIO Code
for($b=count($a=$argv);$b;){$a[0]='1X';$a[--$b]=strtr($a[$b],'ATJQK','1:;<=');$a[]=($a[$b][0]==1?">".$a[$b][1]:1);}asort($a);foreach($a as$c){$d[$c[1]][]=$c[0];}foreach($d as$e){if(4<$g=count($e)){for($h=0;$g>$i=4+$h;){$f|=(ord($e[$i])-ord($e[$h++])==4);}}}echo$f;
Python 2, 95 bytes
lambda a:any(set('A234567891JQKA'[i/4:][:5])<={r['HCSD'[i%4]in r]for r in a}for i in range(40))
There are 40 possible straight flushes, and this simply checks them all. Chas Brown saved 2 bytes; Jo King saved 4 more.
JavaScript (ES6), 106 bytes
h=>h.map(([r,s])=>[..."HSDCA23456789TJQKA"].map(c=>i+=c==s?i*15:c==r?d[i]=1:1,i=0),d=[])|/(,1){5}/.test(d)
Accepts an array of string representations of cards, replacing 10 with T.
Try it online!
Explanation
Iterates over each card and sets a flag in an array of booleans using an index computed from the unique combination of its rank and suit. This array is then stringified to permit matching a pattern of 5 consecutive truthy values.
For example, a hand with a straight flush may produce the following as a
substring of the full string representation of the boolean array: ,,,,1,1,1,1,1,,,,
Because the first rank value (i.e. A) is offset from the
start of the string, there will always be empty values
preceding all 1's in the array, ensuring the string
representation will begin with a ,
h =>
h.map(([r, s]) => // destructure card value, e.g. "JH" => ["J", "H"]
[..."HSDCA23456789TJQKA"].map(c => // mapping accounts for both positions of 'A'
i += // increment index value
c == s // if found index of suit...
? i * 15 // buffer so that cards from different suits cannot be confused
: c == r // if found index of rank...
? d[i] = 1 // set flag to denote card is in hand
: 1,
i = 0
),
d = []
) |
/(,1){5}/.test(d) // implicitly converts to string joined with a ,
Brachylog, 31 bytes
tᵍkᵐ²cᵐ{ps₅~s"A23456789TJQKA"}ᵉ
ᵍ Group input by
t each element's "tail" (i.e. suit)
kᵐ² Knife off the suit character from each element in each array
cᵐ Concatenate the elements of each suit array into a string
{ }ᵉ There exists at least one string in that such that
p it has a permutation
s₅ which has a substring of length 5
~s which is also a substring of
"A23456789JQKA"
Jelly, 18 bytes
Ṣœc5Uµ13R;1wṪ€ȧEµƇ
Actual input format: list ([..., ...]) of lists of 2 integers each; the first is an integer in \$[1,13]\$, for \$A23456789TJQK\$ respectively, and the second is in \$[1,4]\$, for \$CDHS\$ respectively. The TIO link accepts input in the format of the test cases.
Output format: empty list as falsy, non-empty list as truthy.
Clean, 145 135 bytes
import StdEnv,Data.List
?l=or[isInfixOf(map hd h)['A234567891JQKA']\\a<-l,b<-l,c<-l,d<-l,e<-l,h<-[[a,b,c,d,e]]|tl(nub(map last h))==[]]
Simplified:
? l // function ? taking argument l
= or [ // is at least one of these true
isInfixOf (map hd h) ['A234567891JQKA'] // do the first characters of a hand appear in this string, in order
\\ a <- l // loop level 1, assigns `a`
, b <- l // loop level 2, assigns `b`
, c <- l // loop level 3, assigns `c`
, d <- l // loop level 4, assigns `d`
, e <- l // loop level 5, assigns `e`
, h <- [[a,b,c,d,e]] // trick to assign `h`, because it's cheaper than let .. in ..
| tl (nub (map last h)) == [] // only take the loop iterations where all the suits are the same
]
Retina 0.8.2, 66 bytes
J
11
Q
12
K
13
A
1$%'¶14
\d+(.)
$1$&$*
O`
^
¶
((?(1)\1.|¶.+)){5}\b
Try it online! Explanation:
J
11
Q
12
K
13
Convert the picture cards into their values.
A
1$%'¶14
A can be 1 or 14.
\d+(.)
$1$&$*
O`
Convert the value to unary, and suffix it so that the cards sort properly.
^
¶
((?(1)\1.|¶.+)){5}\b
Match 5 cards that increase by 1 each time, and ensure that the last increase was exactly 1.