Skip to content

Instantly share code, notes, and snippets.

@thattommyhall
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thattommyhall/9983615 to your computer and use it in GitHub Desktop.
Save thattommyhall/9983615 to your computer and use it in GitHub Desktop.
(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