View difference between Paste ID: 8dE8Bw49 and q0Z5xBYK
SHOW: | | - or go back to the newest paste.
1-
(defn fullhouse2
1+
(defn n-of-a-kind ;untested
2-
  ([cards]
2+
  [n cards]
3-
     (let [ranked (group-by :rank cards),
3+
  (let [ranked (vals (group-by :rank cards)),
4-
           counted (group-by (comp count second) ranked),
4+
        candidates (filter #(>= (count %) n) ranked),
5-
           {pairs 2, triples 3} (into {} (for [[c r] counted] {c (into {} r)}))]
5+
        highest (max-by #(:rank (first c)) candidates)]
6-
       (when (>= (+ (count pairs) (count triples)) 2)
6+
    {:cards (take n highest), :remaining candidates}))
7-
         (let  [[r triple] (apply max-key first triples)
7+
8-
                paircands (merge pairs (dissoc triples r))
8+
;untested
9-
                [_ pair] (apply max-key first paircands)]
9+
(def pair (partial n-of-a-kind 2))
10-
           (into triple (take 2 pair)))))))
10+
(def triple (partial n-of-a-kind 2))
11
12
(def fullhouse4 ;untested
13
  [cards]
14
  (when-let [{t :cards, remaining :remaining} (triple cards)]
15
    (when-let [{p :cards} (pair remaining)]
16
      (compose t p))))