fork(3) download
  1. (defn not-last-car [x]
  2. "Given percent of reduction, get all the bears, apart from the ones that go to the last car"
  3. (let [prog-mult (- 1 (/ x 100))
  4. take-limit (- 1 prog-mult)]
  5. (take-while #(> % take-limit) (iterate #(* % prog-mult) 1))))
  6.  
  7. (defn partition-bears [bears]
  8. "Given a sequence of bears groups them by cars"
  9. (loop [s bears
  10. acc []]
  11. (cond
  12. (empty? s)
  13. acc
  14. (> 1 (+ (first s) (reduce + (peek acc))))
  15. (recur (rest s) (conj (pop acc) (conj (peek acc) (first s))))
  16. :default
  17. (recur (rest s) (conj acc [(first s)])))))
  18.  
  19. (defn solution [x]
  20. (let [distribution (-> x not-last-car partition-bears)
  21. size (count distribution)]
  22. (println "Cars required" (inc size) "\n"
  23. (apply str (map-indexed #(str (inc %1) ":" %2 "\n") distribution))
  24. (str (inc size) ":all other bears"))))
  25.  
  26. (solution 10)
  27. (solution 20)
Success #stdin #stdout 1.44s 389120KB
stdin
Standard input is empty
stdout
Cars required 13 
 1:[1]
2:[9/10]
3:[81/100]
4:[729/1000]
5:[6561/10000]
6:[59049/100000]
7:[531441/1000000]
8:[4782969/10000000 43046721/100000000]
9:[387420489/1000000000 3486784401/10000000000]
10:[31381059609/100000000000 282429536481/1000000000000 2541865828329/10000000000000]
11:[22876792454961/100000000000000 205891132094649/1000000000000000 1853020188851841/10000000000000000 16677181699666569/100000000000000000 150094635296999121/1000000000000000000]
12:[1350851717672992089/10000000000000000000 12157665459056928801/100000000000000000000 109418989131512359209/1000000000000000000000]
 13:all other bears
Cars required 6 
 1:[1]
2:[4/5]
3:[16/25]
4:[64/125 256/625]
5:[1024/3125 4096/15625 16384/78125]
 6:all other bears