Skip to content

Instantly share code, notes, and snippets.

@onemouth
Created September 6, 2015 07:35
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 onemouth/d5273afdc875b2275279 to your computer and use it in GitHub Desktop.
Save onemouth/d5273afdc875b2275279 to your computer and use it in GitHub Desktop.
(require '[clojure.string :as str])
(defrecord Coffee [water ground])
(defn return-monad-coffee [water ground]
(fn []
(Coffee. water ground)))
(defn bind-monad-coffee [m-coffee f]
(f (m-coffee)))
(defn add-water [coffee water-to-add]
(let [{:keys [water ground]} coffee]
(return-monad-coffee (+ water water-to-add) ground)))
(defn m-add-water [m-coffee water-to-add]
(bind-monad-coffee m-coffee
#(add-water % water-to-add)))
(defn add-ground [coffee ground-to-add]
(let [{:keys [water ground]} coffee]
(return-monad-coffee water (+ ground ground-to-add))))
(defn m-add-ground [m-coffee ground-to-add]
(bind-monad-coffee m-coffee
#(add-ground % ground-to-add)))
(defn take-coffee [coffee g]
(let [{:keys [water ground]} coffee
total (+ water ground)
minus-water (* g (/ water total))
minus-ground (* g (/ ground total))]
(return-monad-coffee (- water minus-water) (- ground minus-ground))))
(defn m-take-coffee [m-coffee g]
(bind-monad-coffee m-coffee
#(take-coffee % g)))
(defn ground-percent [coffee]
(let [{:keys [water ground]} coffee]
(int (Math/floor (* 100 (double (/ ground (+ water ground))))))))
(defn action-m-coffee [m-coffee action num]
(condp = action
1 (m-add-water m-coffee num)
2 (m-add-ground m-coffee num)
3 (m-take-coffee m-coffee num)))
(defn procedure-m-coffee [m-coffee procedure]
(loop
[m-coffee m-coffee
procedure procedure]
(cond
(nil? (seq procedure)) m-coffee
:else (recur (action-m-coffee m-coffee
(get (first procedure) 0)
(get (first procedure) 1))
(rest procedure)))))
(defn parse-str-to-cells [str-cells]
(->> (str/split str-cells #"\s+")
(mapv #(Integer. %))))
(let [num-of-input (Integer. (read-line))
m-coffee (return-monad-coffee 0 0)
procedure (for [x (range num-of-input)] (parse-str-to-cells (read-line)))
after-coffee (procedure-m-coffee m-coffee procedure)]
(println (ground-percent (after-coffee))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment