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

master
Clyne 3 years ago
parent d4a5814cdc
commit 42db1403e2

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

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

Loading…
Cancel
Save