Add Clojure translation

This commit is contained in:
Paulus Esterhazy committed 2022-09-25 20:41:30 +02:00
1 parent bdcea60169
commit c81d02acf5
7 files changed
+138

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
(ns gilded.core)
(defn make-store [items]
(assert (vector? items))
(->> items
(map (fn [item] (atom item)))
vec))
(defn item-seq [store]
(->> store
(map deref)))
;; ---
(defn update-quality! [store]
(doseq [item store]
(if (and (not (= (:name @item)
"Aged Brie"))
(not (= (:name @item)
"Backstage passes to a TAFKAL80ETC concert")))
(when (> (:quality @item) 0)
(when (not (= (:name @item) "Sulfuras, Hand of Ragnaros"))
(swap! item update :quality #(- % 1))))
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1))
(when (= (:name @item) "Backstage passes to a TAFKAL80ETC concert")
(when (< (:sell-in @item) 11)
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1))))
(when (< (:sell-in @item) 6)
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1)))))))
(when (not (= (:name @item) "Sulfuras, Hand of Ragnaros"))
(swap! item update :sell-in #(- % 1)))
(when (< (:sell-in @item) 0)
(if (not (= (:name @item) "Aged Brie"))
(if (not (= (:name @item) "Backstage passes to a TAFKAL80ETC concert"))
(when (> (:quality @item) 0)
(when (not (= (:name @item) "Sulfuras, Hand of Ragnaros"))
(swap! item update :quality #(- % 1))))
(swap! item update :quality #(- % %)))
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1)))))))
+32
View File
@@ -0,0 +1,32 @@
(ns gilded.main
(:require [gilded.core :as x]))
(defn item->str [item]
(str (:name item) ", " (:sell-in item) ", " (:quality item)))
(defn print-store [store]
(println "name, sellIn, quality")
(doseq [item (x/item-seq store)]
(println (item->str item)))
(println))
(def fixture
[{:name "+5 Dexterity Vest", :quality 20, :sell-in 10}
{:name "Aged Brie", :quality 0, :sell-in 2}
{:name "Elixir of the Mongoose", :quality 7, :sell-in 5}
{:name "Sulfuras, Hand of Ragnaros", :quality 80, :sell-in 0}
{:name "Sulfuras, Hand of Ragnaros", :quality 80, :sell-in -1}
{:name "Backstage passes to a TAFKAL80ETC concert", :quality 20, :sell-in 15}
{:name "Backstage passes to a TAFKAL80ETC concert", :quality 49, :sell-in 10}
{:name "Backstage passes to a TAFKAL80ETC concert", :quality 49, :sell-in 5}
{:name "Conjured Mana Cake", :quality 6, :sell-in 3}])
(defn -main [& args]
(let [n-days (if (nil? (first args))
2
(Long/parseLong (first args)))
store (x/make-store fixture)]
(dotimes [day n-days]
(println "-------- day" day "--------")
(print-store store)
(x/update-quality! store))))