From: Clyne Sullivan Date: Fri, 10 Dec 2021 15:24:47 +0000 (-0500) Subject: day10: learn if-let, when-let, some->> X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=42db1403e269fad7dfa1656b6c5cc4cda2dd6b14;p=clyne%2Fadvent-of-code.git day10: learn if-let, when-let, some->> --- diff --git a/day10/part1.clj b/day10/part1.clj index 1424fd8..60ed1e6 100644 --- a/day10/part1.clj +++ b/day10/part1.clj @@ -3,19 +3,15 @@ (defn check-line [input] (loop [in input open '()] - (cond - (empty? in) - nil - (and (contains? #{\} \) \] \>} (first in)) - (not= (to-closing (first open)) (first in))) - (first in) - :else - (recur - (rest in) - (if (contains? #{\{ \( \[ \<} (first in)) - (conj open (first in)) - (rest open) - ))))) + (when-let [c (first in)] + (if (some->> (#{\} \) \] \>} c) (not= (first open))) + c + (recur + (rest in) + (if-let [op (to-closing c)] + (conj open op) + (rest open) + )))))) (->> (slurp "./in") (clojure.string/split-lines) diff --git a/day10/part2.clj b/day10/part2.clj index 49c9136..4dfaf3b 100644 --- a/day10/part2.clj +++ b/day10/part2.clj @@ -3,19 +3,15 @@ (defn check-line [input] (loop [in input open '()] - (cond - (empty? in) - (map to-closing open) - (and (contains? #{\} \) \] \>} (first in)) - (not= (to-closing (first open)) (first in))) - nil - :else - (recur - (rest in) - (if (contains? #{\{ \( \[ \<} (first in)) - (conj open (first in)) - (rest 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)