Skip to content

Instantly share code, notes, and snippets.

Created May 9, 2013 19:44
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 anonymous/5550022 to your computer and use it in GitHub Desktop.
Save anonymous/5550022 to your computer and use it in GitHub Desktop.
(defn lazy-compare
[& s]
(-> (some #{-1 1} (apply map compare s)) (or 0)))
(defn canonical?
[cards]
(if (not (= (sort cards) cards))
false
(let [by-suits (reduce (fn [a-map suit]
(let [new-val (for [card cards
:when (and (>= card suit)
(< card (+ suit 13)))]
(mod card 13))]
(if (= (count (into #{} new-val)) (count new-val))
(merge-with concat a-map {suit new-val})
(reduced false))))
{}
(range 0 52 13))]
(and by-suits
(not-any? (fn [suit]
(let [suit1 (by-suits (- suit 13))
suit2 (by-suits suit)]
(and (not (empty? suit2))
(or (< (count suit1) (count suit2))
(and (= (count suit1) (count suit2))
(pos? (lazy-compare suit1 suit2)))))))
(range 13 52 13))))))
(defn deal-cards
[perms n cards]
(if (= (count cards) n)
(concat perms cards))
(letfn [(deal-cards* [perms n cards]
(if (= (count cards) n)
(concat perms cards)
(let [start (if (not (empty? cards))
(+ (reduce max cards) 1)
0)]
(filter (comp not empty?)
(map (fn [card]
(let [cards* (concat cards [card])]
(if (canonical? cards*)
(deal-cards* perms n cards*))))
(range start 52))))))]
(deal-cards* perms n cards)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment