diff --git a/2017/src/day08.clj b/2017/src/day08.clj new file mode 100644 index 0000000..3f0ce18 --- /dev/null +++ b/2017/src/day08.clj @@ -0,0 +1,46 @@ +(ns day08 + (:require + [clojure.string :as str] + [core] + [input-manager])) + +(def op {"inc" + + "dec" -}) +(def cond-op {"<" < + "<=" <= + ">" > + ">=" >= + "==" = + "!=" not=}) +(def input (map (comp + (fn [[name operation num _ cond-name cond-operation cond-val]] + [name + (op operation) + (parse-long num) + cond-name + (cond-op cond-operation) + (parse-long cond-val)]) + core/split-whitespace) + (input-manager/get-input 2017 8))) + +(defn compute [acc [reg reg-op val cond-reg cond-op cond-val]] + (if (cond-op (acc cond-reg 0) cond-val) + (assoc acc reg (reg-op (acc reg 0) val)) + acc)) + +;; part 1 +(->> input + (reduce compute {}) + (sort-by second) + last + second) + +;; part 2 +(->> input + (reductions compute {}) + (mapcat identity) + set + (sort-by second) + last + second) +