]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day10: add partboth.clj
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Dec 2021 17:14:31 +0000 (12:14 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 10 Dec 2021 17:14:31 +0000 (12:14 -0500)
day10/partboth.clj [new file with mode: 0644]

diff --git a/day10/partboth.clj b/day10/partboth.clj
new file mode 100644 (file)
index 0000000..78080d9
--- /dev/null
@@ -0,0 +1,34 @@
+(def to-closing {\{ \} \( \) \[ \] \< \>})
+(def to-score {\) 3 \] 57 \} 1197 \> 25137})
+(def to-score2 {\) 1 \] 2 \} 3 \> 4})
+
+(defn check-line [input]
+  (loop [in input open '()]
+    (if-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)
+            )))
+      open
+      )))
+
+(->> (slurp "./in")
+     (clojure.string/split-lines)
+     (map (comp check-line vec))
+     ; check-line returns first invalid character, or list of characters
+     ; necessary to close the line. Work through these through `reduce`
+     ; and build the answers for both parts:
+     (reduce
+       (fn [tots nxt]
+         (if (seq? nxt)
+           (update tots 1 #(conj % (reduce (fn [a b] (+ (* 5 a) (to-score2 b))) 0 nxt)))
+           (update tots 0 #(+ % (to-score nxt)))))
+       [0 '()])
+     ; Get part 2 answer from the list of scores:
+     (#(update % 1 (fn [ns] (nth ns (quot (count ns) 2)))))
+     (println))
+