day10: learn if-let, when-let, some->>

master
Clyne 3 years ago
parent d4a5814cdc
commit 42db1403e2

@ -3,19 +3,15 @@
(defn check-line [input] (defn check-line [input]
(loop [in input open '()] (loop [in input open '()]
(cond (when-let [c (first in)]
(empty? in) (if (some->> (#{\} \) \] \>} c) (not= (first open)))
nil c
(and (contains? #{\} \) \] \>} (first in))
(not= (to-closing (first open)) (first in)))
(first in)
:else
(recur (recur
(rest in) (rest in)
(if (contains? #{\{ \( \[ \<} (first in)) (if-let [op (to-closing c)]
(conj open (first in)) (conj open op)
(rest open) (rest open)
))))) ))))))
(->> (slurp "./in") (->> (slurp "./in")
(clojure.string/split-lines) (clojure.string/split-lines)

@ -3,19 +3,15 @@
(defn check-line [input] (defn check-line [input]
(loop [in input open '()] (loop [in input open '()]
(cond (if-let [c (first in)]
(empty? in) (when (or (nil? (#{\} \) \] \>} c)) (= (first open) c))
(map to-closing open)
(and (contains? #{\} \) \] \>} (first in))
(not= (to-closing (first open)) (first in)))
nil
:else
(recur (recur
(rest in) (rest in)
(if (contains? #{\{ \( \[ \<} (first in)) (if-let [op (to-closing c)]
(conj open (first in)) (conj open op)
(rest open) (rest open))))
))))) open
)))
(->> (slurp "./in") (->> (slurp "./in")
(clojure.string/split-lines) (clojure.string/split-lines)

Loading…
Cancel
Save