Skip to content

Instantly share code, notes, and snippets.

@dmarjenburgh
Forked from skuro/README.md
Last active March 9, 2016 20:15
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 dmarjenburgh/505faf5e07373215f8f4 to your computer and use it in GitHub Desktop.
Save dmarjenburgh/505faf5e07373215f8f4 to your computer and use it in GitHub Desktop.
Lotsa advocaat

Amsterdam Clojurians dojo n. 76

Fork this gist and provide your solution. Input is:

50
44
11
49
42
46
18
32
26
40
21
7
18
43
10
47
36
24
22
40
(ns advocaat
(:require [loco.core :refer :all]
[loco.constraints :refer :all]))
(def small-input [50 44 11 49 42 46 18 32 26 40 21 7 18 43 10 47 36 24 22 40])
(def input (vec (repeatedly 32 #(rand-int 50))))
(defn solve [input]
(let [num-containers (count input)
target 150
vars (map (fn [i] [:x i]) (range num-containers))
range-constraints (map (fn [cv] ($in cv 0 1)) vars)
sum-constraint ($= ($scalar vars input) target)
sols (solutions (conj range-constraints sum-constraint))
container-sizes (fn [sol] (sort (mapcat (fn [[[_ i] v]]
(when (= v 1) [(input i)])) sol)))
nice-sols (map container-sizes sols)]
(assert (every? (comp #{target} (partial apply +)) nice-sols))
(when (seq nice-sols)
{:different-permutations (count nice-sols)
:unique-permutations (count (into #{} nice-sols))
:smallest-solution (let [num (count (apply min-key count nice-sols))]
(count (filter (comp #{num} count) nice-sols)))
:first-three (take 3 nice-sols)})))
(comment
(time (solve input))
(solve small-input)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment