Skip to content

Instantly share code, notes, and snippets.

@dabd
Created January 26, 2013 20:08
Show Gist options
  • Save dabd/4644325 to your computer and use it in GitHub Desktop.
Save dabd/4644325 to your computer and use it in GitHub Desktop.
(defn get-hand-ranks
[h]
(cond (or (= (count h) 2)
(re-seq #"^..s" h)
(re-seq #"..o" h))
[(first h) (second h)]
(or (re-seq #".[hcsd].[hcsd]" h)
(re-seq #".x.x" h)
(re-seq #".x.y" h))
[(first h) (nth h 2)]
:else (throw (Exception. (str "Invalid hand: " h)))))
(defn sort-by-rank-order
[hs]
(let [rank-order "AKQJT98765432"]
(sort (fn [h1 h2]
(let [[h1r1 h1r2] (get-hand-ranks h1)
[h2r1 h2r2] (get-hand-ranks h2)]
(and (<= (first (positions #(= % h1r1) rank-order))
(first (positions #(= % h2r1) rank-order)))
(<= (first (positions #(= % h1r2) rank-order))
(first (positions #(= % h2r2) rank-order))))))
hs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment