Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created December 6, 2014 18:32
Show Gist options
  • Save mwmitchell/7569f28b3f5ac9538fdc to your computer and use it in GitHub Desktop.
Save mwmitchell/7569f28b3f5ac9538fdc to your computer and use it in GitHub Desktop.
xmas gift swap
(require '[clojure.string :as str])
(require '[clojure.set :as set])
(require '[clojure.pprint :as pp])
(defn map-names [names]
(first
(reduce
(fn [mem n1]
(let [avail (set/difference names (get mem 1))
sel (->> avail shuffle (some #(when (not= n1 %1) %1)))]
(-> mem
(assoc-in [0 n1] sel)
(update-in [1] conj sel))))
[{} #{}] names)))
(pp/pprint (map-names #{"barney" "fred" "wilma" "betty" "pebbles" "dino" "bamm bamm" "baby puss" "hoppy"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment