Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Clojure runner #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clojure/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
:dependencies [[org.clojure/clojure "1.8.0"]]
:profiles {:dev {:dependencies [[speclj "3.3.2"]]}}
:plugins [[speclj "3.3.2"]]
:test-paths ["spec"])
:test-paths ["spec"]
:main gilded-rose.runner)
47 changes: 22 additions & 25 deletions clojure/src/gilded_rose/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@

(defn update-quality [items]
(map
(fn[item] (cond
(and (< (:sell-in item) 0) (= "Backstage passes to a TAFKAL80ETC concert" (:name item)))
(merge item {:quality 0})
(or (= (:name item) "Aged Brie") (= (:name item) "Backstage passes to a TAFKAL80ETC concert"))
(if (and (= (:name item) "Backstage passes to a TAFKAL80ETC concert") (>= (:sell-in item) 5) (< (:sell-in item) 10))
(merge item {:quality (inc (inc (:quality item)))})
(if (and (= (:name item) "Backstage passes to a TAFKAL80ETC concert") (>= (:sell-in item) 0) (< (:sell-in item) 5))
(merge item {:quality (inc (inc (inc (:quality item))))})
(if (< (:quality item) 50)
(merge item {:quality (inc (:quality item))})
item)))
(< (:sell-in item) 0)
(if (= "Backstage passes to a TAFKAL80ETC concert" (:name item))
(merge item {:quality 0})
(if (or (= "+5 Dexterity Vest" (:name item)) (= "Elixir of the Mongoose" (:name item)))
(merge item {:quality (- (:quality item) 2)})
item))
(or (= "+5 Dexterity Vest" (:name item)) (= "Elixir of the Mongoose" (:name item)))
(merge item {:quality (dec (:quality item))})
:else item))
(map (fn [item]
(if (not= "Sulfuras, Hand of Ragnaros" (:name item))
(merge item {:sell-in (dec (:sell-in item))})
item))
items)))
(fn [item] (cond
(and (< (:sell-in item) 0) (= "Backstage passes to a TAFKAL80ETC concert" (:name item)))
(merge item {:quality 0})
(or (= (:name item) "Aged Brie") (= (:name item) "Backstage passes to a TAFKAL80ETC concert"))
(if (and (= (:name item) "Backstage passes to a TAFKAL80ETC concert") (>= (:sell-in item) 5) (< (:sell-in item) 10))
(merge item {:quality (inc (inc (:quality item)))})
(if (and (= (:name item) "Backstage passes to a TAFKAL80ETC concert") (>= (:sell-in item) 0) (< (:sell-in item) 5))
(merge item {:quality (inc (inc (inc (:quality item))))})
(if (< (:quality item) 50)
(merge item {:quality (inc (:quality item))})
item)))
(< (:sell-in item) 0)
(if (= "Backstage passes to a TAFKAL80ETC concert" (:name item))
(merge item {:quality 0})
(merge item {:quality (- (:quality item) (if (zero? (:quality item)) (:quality item) 2))}))
:else
(merge item {:quality (max 0 (dec (:quality item)))})))
(map (fn [item]
(if (not= "Sulfuras, Hand of Ragnaros" (:name item))
(merge item {:sell-in (dec (:sell-in item))})
(merge item {:quality (inc (:quality item))})))
items)))

(defn item [item-name, sell-in, quality]
{:name item-name, :sell-in sell-in, :quality quality})
Expand Down
28 changes: 28 additions & 0 deletions clojure/src/gilded_rose/runner.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns gilded-rose.runner
(:require [clojure.string :as str]
[gilded-rose.core :as grc]))
(defn -main
[& args]
(when (< (count args) 1)
(do
(.println System/out "Please supply test file")
(System/exit 1)))
(when (< (count args) 2)
(do
(.println System/out "Please supply out file location")
(System/exit 1)))

(let [test-file (first args)
out-file (second args)]
(.println System/out (str "Reading from: " test-file ". Writing to: " out-file))
(let [test-input (slurp test-file)
inventory-strs (filter (fn [s] (not (or (empty? s) (str/starts-with? s ";"))))
(str/split test-input #"\n"))
inventory (map #(let [[name sell-in quality] (str/split % #"__")]
(grc/item name (Integer/parseInt sell-in) (Integer/parseInt quality)))
inventory-strs)
new-inventory (grc/update-quality inventory)
new-inventory-str (str/join "\n" (map (fn [{:keys [name sell-in quality]}]
(str/join "__" [name sell-in quality]))
new-inventory))]
(spit out-file new-inventory-str))))