]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day10: learn if-let, when-let, some->>
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Dec 2021 15:24:47 +0000 (10:24 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Dec 2021 15:24:47 +0000 (10:24 -0500)
day10/part1.clj
day10/part2.clj

index 1424fd8a230669d0eeb3c74a32c4895d1c60c60d..60ed1e68fa711d4c928ca1bbbf407cef367f17ec 100644 (file)
@@ -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)
index 49c913604ba6038ca339fe29fe3135aefecb6973..4dfaf3bd8460fa6888ecedfc4d619d80779b44f9 100644 (file)
@@ -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)