From d4a5814cdc196c72ccdaa5ee9e3dea67bf45419e Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 10 Dec 2021 08:15:02 -0500 Subject: [PATCH] day9: clean up --- day10/part1.clj | 14 ++++++-------- day10/part2.clj | 20 +++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/day10/part1.clj b/day10/part1.clj index 2bef696..1424fd8 100644 --- a/day10/part1.clj +++ b/day10/part1.clj @@ -2,7 +2,7 @@ (def to-score {\) 3 \] 57 \} 1197 \> 25137}) (defn check-line [input] - (loop [open [] in input] + (loop [in input open '()] (cond (empty? in) nil @@ -11,18 +11,16 @@ (first in) :else (recur - (if (contains? #{\{ \( \[ \<} (first in)) - (concat [(first in)] open) - (rest open)) (rest in) - )))) + (if (contains? #{\{ \( \[ \<} (first in)) + (conj open (first in)) + (rest open) + ))))) (->> (slurp "./in") (clojure.string/split-lines) - (mapv vec) - (mapv check-line) + (map (comp to-score check-line vec)) (filter some?) - (mapv to-score) (apply +) (println)) diff --git a/day10/part2.clj b/day10/part2.clj index 6cd27af..49c9136 100644 --- a/day10/part2.clj +++ b/day10/part2.clj @@ -2,7 +2,7 @@ (def to-score {\) 1 \] 2 \} 3 \> 4}) (defn check-line [input] - (loop [open [] in input] + (loop [in input open '()] (cond (empty? in) (map to-closing open) @@ -11,20 +11,18 @@ nil :else (recur - (if (contains? #{\{ \( \[ \<} (first in)) - (concat [(first in)] open) - (rest open)) (rest in) - )))) + (if (contains? #{\{ \( \[ \<} (first in)) + (conj open (first in)) + (rest open) + ))))) (->> (slurp "./in") (clojure.string/split-lines) - (mapv vec) - (mapv check-line) + (map (comp check-line vec)) (filter some?) - (mapv (partial reduce #(+ (* 5 %1) (to-score %2)) 0)) + (map (partial reduce #(+ (* 5 %1) (to-score %2)) 0)) (sort) - (#(get (vec %) (quot (count %) 2))) - (println) - ) + (#(nth % (quot (count %) 2))) + (println))