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) | |
) |