Skip to content

Instantly share code, notes, and snippets.

@mishadoff
Last active August 29, 2015 13:56
Show Gist options
  • Save mishadoff/9089925 to your computer and use it in GitHub Desktop.
Save mishadoff/9089925 to your computer and use it in GitHub Desktop.
(defn solve [r t]
"r - initial radius, t - mls of paint"
(letfn [(nrings [n]
(+' (*' 2 n n)
(-' n)
(*' 2 r n)))
(binary [[a b]]
(cond (or (= a b) (= (inc a) b)) a
:else (let [avg (quot (+' a b) 2)
pnt (nrings avg)]
(if (<= pnt t)
(recur [avg b])
(recur [a avg])))))]
(binary [0 1000000000000000000])))
;; bulllseye> (time (do (println (solve 1 9))
;; (println (solve 1 10))
;; (println (solve 3 40))
;; (println (solve 1 1000000000000000000))
;; (println (solve 10000000000000000 1000000000000000000))
;; :ok))
;; 1
;; 2
;; 3
;; 707106780
;; 49
;; "Elapsed time: 4.398956 msecs"
;; :ok
(defn solve-file [in out]
(->> (slurp in)
(re-seq #"\d+")
(map bigint)
(rest)
(partition 2 2)
(map-indexed #(vec [(inc %1) (apply solve %2)]))
(map #(format "Case #%d: %d" (first %) (last %)))
(interpose "\n")
(apply str)
(spit out)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment