Last active
August 29, 2015 13:58
-
-
Save thattommyhall/9983615 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(fn [cards] | |
(let [card-type (fn [input] | |
(let [[suit rank] (rest | |
(clojure.string/split input #"")) | |
suits {"D" :diamond | |
"H" :heart | |
"C" :club} | |
ranks (zipmap (map | |
str '[2 3 4 5 6 7 8 9 T J Q K A]) | |
(range 13))] | |
[ (suits suit) | |
(ranks rank)])) | |
cards (map card-type cards) | |
suits (set (map first cards)) | |
rank-frequencies (frequencies (map second cards)) | |
ranks (sort ( keys rank-frequencies)) | |
rank-counts (sort (vals rank-frequencies)) | |
flush? (= 1 (count suits)) | |
straight? (and (= 5 (count ranks)) | |
(or (= (apply max ranks) | |
(+ 4 (apply min ranks))) | |
(= [0 1 2 3 12] ranks)))] | |
(cond (and straight? flush?) | |
:straight-flush | |
(some #(= 4 %) rank-counts) | |
:four-of-a-kind | |
(= [2 3] rank-counts) | |
:full-house | |
flush? | |
:flush | |
straight? | |
:straight | |
(some #(= 3 %) rank-counts) | |
:three-of-a-kind | |
(= 2 (count (filter #(= 2 %) rank-counts))) | |
:two-pair | |
(some #(= 2 %) rank-counts) | |
:pair | |
:else | |
:high-card))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment