aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/aocclj/src
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-02-11 19:34:20 +0100
committerOscar Najera <hi@oscarnajera.com>2023-02-11 19:34:20 +0100
commitefcc7c32228d87f9f41cd5963491e9d11ee30dfb (patch)
tree3768cd36876edc6b8b53621244d8a0803569f053 /AoC2022/aocclj/src
parent1a0a9714f95767bdad046dbf32182967fcefb5f9 (diff)
downloadscratch-efcc7c32228d87f9f41cd5963491e9d11ee30dfb.tar.gz
scratch-efcc7c32228d87f9f41cd5963491e9d11ee30dfb.tar.bz2
scratch-efcc7c32228d87f9f41cd5963491e9d11ee30dfb.zip
Clojure day05
Diffstat (limited to 'AoC2022/aocclj/src')
-rw-r--r--AoC2022/aocclj/src/aocclj/day05.clj38
1 files changed, 38 insertions, 0 deletions
diff --git a/AoC2022/aocclj/src/aocclj/day05.clj b/AoC2022/aocclj/src/aocclj/day05.clj
new file mode 100644
index 0000000..09317a8
--- /dev/null
+++ b/AoC2022/aocclj/src/aocclj/day05.clj
@@ -0,0 +1,38 @@
+(ns aocclj.day05
+ (:require [clojure.string :as str]))
+
+(defn parse-instruction [line]
+ (->> (re-seq #"\d+" line)
+ (map parse-long)
+ (apply (fn [move from to]
+ [move (dec from) (dec to)]))))
+
+(defn parse-input [input]
+ (let [[stacks instructions] (str/split input #"\n\n")]
+ {:stacks
+ (mapv
+ (fn [col] (filter #(not= % \space) col))
+ (apply mapv vector
+ (map (comp (partial map second)
+ (partial partition-all 4))
+ (butlast (str/split-lines stacks)))))
+ :instructions
+ (map parse-instruction (str/split-lines instructions))}))
+
+(defn mover [f stacks [amount from to]]
+ (let [stuff (f (take amount (get stacks from)))]
+ (-> stacks
+ (update to #(apply conj % stuff))
+ (update from (partial drop amount)))))
+
+(defn solver [single? input]
+ (let [{:keys [stacks instructions]}
+ (parse-input input)]
+ (->>
+ (reduce
+ (partial mover (if single? identity reverse))
+ stacks
+ instructions)
+ (map first)
+ (apply str))))
+