Skip to content

Instantly share code, notes, and snippets.

@orendon
Forked from sarcilav/mini.clj
Created December 21, 2016 16:03
Show Gist options
  • Save orendon/99c7ba3c24160aa140f9c9f39a476660 to your computer and use it in GitHub Desktop.
Save orendon/99c7ba3c24160aa140f9c9f39a476660 to your computer and use it in GitHub Desktop.
(defn permutations [s]
(lazy-seq
(if (seq (rest s))
(apply concat (for [x s]
(map #(cons x %) (permutations (remove #{x} s)))))
[s])))
(defn cost [proposoals s]
{:permutation s
:cost (apply + (map-indexed (fn [project candidate-str]
(let [[candidate _] (clojure.string/split candidate-str #"-")]
(get-in proposoals [candidate project])))
s))})
(defn basic-seq [candidates max-per]
(let [opts (range 0 max-per)]
(flatten (map (fn [c]
(map (fn [opt] (str c "-" opt)) opts))
candidates))))
(defn mini [proposoals max-per]
(apply min-key :cost (map (partial cost proposoals) (permutations (basic-seq (keys proposoals) max-per)))))
(mini {"c1" [1 2 30 4] "c2" [4 3 200 1]} 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment