aboutsummaryrefslogtreecommitdiffstats
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
parent1a0a9714f95767bdad046dbf32182967fcefb5f9 (diff)
downloadscratch-efcc7c32228d87f9f41cd5963491e9d11ee30dfb.tar.gz
scratch-efcc7c32228d87f9f41cd5963491e9d11ee30dfb.tar.bz2
scratch-efcc7c32228d87f9f41cd5963491e9d11ee30dfb.zip
Clojure day05
-rw-r--r--AoC2022/05/solver.el26
-rw-r--r--AoC2022/aocclj/src/aocclj/day05.clj38
-rw-r--r--AoC2022/aocclj/test/aocclj/core_test.clj7
3 files changed, 55 insertions, 16 deletions
diff --git a/AoC2022/05/solver.el b/AoC2022/05/solver.el
index ed42bb2..b81e4be 100644
--- a/AoC2022/05/solver.el
+++ b/AoC2022/05/solver.el
@@ -15,6 +15,7 @@
;;
;;; Code:
(require 'subr-x)
+(require 'cl-lib)
(defun solver-build-stacks ()
(let* ((stacks-len (length
@@ -27,38 +28,31 @@
(aref stacks (/ (- next (line-beginning-position) 1) 4))))
stacks))
-(defun solver-single-move! (stacks amount from to)
- (dotimes (_ amount)
- (push (pop (aref stacks from)) (aref stacks to))))
-
-(defun solver-bulk-move! (stacks amount from to)
- (setf (aref stacks to)
- (append (seq-take (aref stacks from) amount)
- (aref stacks to)))
- (setf (aref stacks from) (seq-drop (aref stacks from) amount)))
-
-(defun solver-apply-moves (execution stacks)
+(defun solver-apply-moves! (mover-f stacks)
(while (re-search-forward (rx bol "move " (group (+ digit)) " from " (group (+ digit)) " to " (group (+ digit))) nil t)
(let ((amount (string-to-number (match-string 1)))
(from (1- (string-to-number (match-string 2))))
(to (1- (string-to-number (match-string 3)))))
- (funcall execution stacks amount from to)))
+ (setf (aref stacks to)
+ (append (funcall mover-f (seq-take (aref stacks from) amount))
+ (aref stacks to)))
+ (setf (aref stacks from) (seq-drop (aref stacks from) amount))))
stacks)
-(defun solver (execution)
+(defun solver (bulkp)
(with-temp-buffer
(insert-file-contents "input")
(re-search-forward "^ 1")
(thread-last
(solver-build-stacks)
- (solver-apply-moves execution)
+ (solver-apply-moves! (if bulkp #'identity #'reverse))
(cl-map 'string #'car))))
(ert-deftest solver-solutions ()
(should (equal "TPGVQPFDH"
- (solver #'solver-single-move!)))
+ (solver nil)))
(should (equal "DMRDFRHHH"
- (solver #'solver-bulk-move!))))
+ (solver t))))
(provide 'solver)
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))))
+
diff --git a/AoC2022/aocclj/test/aocclj/core_test.clj b/AoC2022/aocclj/test/aocclj/core_test.clj
index c42285f..38e4f72 100644
--- a/AoC2022/aocclj/test/aocclj/core_test.clj
+++ b/AoC2022/aocclj/test/aocclj/core_test.clj
@@ -4,6 +4,7 @@
[aocclj.day02 :as day02]
[aocclj.day03 :as day03]
[aocclj.day04 :as day04]
+ [aocclj.day05 :as day05]
[aocclj.core :refer :all]
[clojure.string :as str]))
@@ -30,3 +31,9 @@
(are [expected function] (= expected (day04/solver function input))
515 day04/subcontained
883 day04/overlap)))
+
+(deftest day05
+ (let [input (slurp "../05/input")]
+ (are [expected single] (= expected (day05/solver single input))
+ "TPGVQPFDH" true
+ "DMRDFRHHH" false)))