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)) (recur
(not= (to-closing (first open)) (first in))) (rest in)
(first in) (if-let [op (to-closing c)]
:else (conj open op)
(recur (rest open)
(rest in) ))))))
(if (contains? #{\{ \( \[ \<} (first in))
(conj open (first in))
(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) (recur
(and (contains? #{\} \) \] \>} (first in)) (rest in)
(not= (to-closing (first open)) (first in))) (if-let [op (to-closing c)]
nil (conj open op)
:else (rest open))))
(recur open
(rest in) )))
(if (contains? #{\{ \( \[ \<} (first in))
(conj open (first in))
(rest open)
)))))
(->> (slurp "./in") (->> (slurp "./in")
(clojure.string/split-lines) (clojure.string/split-lines)

Loading…
Cancel
Save