From 8d43e37df99f280377bed90284d6ac2428334804 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 30 Nov 2022 19:55:31 -0500 Subject: move 2021 days to folder; update README --- year2021/day10/part2.clj | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 year2021/day10/part2.clj (limited to 'year2021/day10/part2.clj') diff --git a/year2021/day10/part2.clj b/year2021/day10/part2.clj new file mode 100644 index 0000000..4dfaf3b --- /dev/null +++ b/year2021/day10/part2.clj @@ -0,0 +1,24 @@ +(def to-closing {\{ \} \( \) \[ \] \< \>}) +(def to-score {\) 1 \] 2 \} 3 \> 4}) + +(defn check-line [input] + (loop [in input open '()] + (if-let [c (first in)] + (when (or (nil? (#{\} \) \] \>} c)) (= (first open) c)) + (recur + (rest in) + (if-let [op (to-closing c)] + (conj open op) + (rest open)))) + open + ))) + +(->> (slurp "./in") + (clojure.string/split-lines) + (map (comp check-line vec)) + (filter some?) + (map (partial reduce #(+ (* 5 %1) (to-score %2)) 0)) + (sort) + (#(nth % (quot (count %) 2))) + (println)) + -- cgit v1.2.3