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

diff --git a/day10/part1.clj b/day10/part1.clj
new file mode 100644 (file)
index 0000000..2bef696
--- /dev/null
@@ -0,0 +1,28 @@
+(def to-closing {\{ \} \( \) \[ \] \< \>})
+(def to-score {\) 3 \] 57 \} 1197 \> 25137})
+
+(defn check-line [input]
+  (loop [open [] in input]
+    (cond
+      (empty? in)
+      nil
+      (and (contains? #{\} \) \] \>} (first in))
+           (not= (to-closing (first open)) (first in)))
+      (first in)
+      :else
+      (recur
+        (if (contains? #{\{ \( \[ \<} (first in))
+          (concat [(first in)] open)
+          (rest open))
+        (rest in)
+        ))))
+
+(->> (slurp "./in")
+     (clojure.string/split-lines)
+     (mapv vec)
+     (mapv check-line)
+     (filter some?)
+     (mapv to-score)
+     (apply +)
+     (println))
+
diff --git a/day10/part2.clj b/day10/part2.clj
new file mode 100644 (file)
index 0000000..6cd27af
--- /dev/null
@@ -0,0 +1,30 @@
+(def to-closing {\{ \} \( \) \[ \] \< \>})
+(def to-score {\) 1 \] 2 \} 3 \> 4})
+
+(defn check-line [input]
+  (loop [open [] in input]
+    (cond
+      (empty? in)
+      (map to-closing open)
+      (and (contains? #{\} \) \] \>} (first in))
+           (not= (to-closing (first open)) (first in)))
+      nil
+      :else
+      (recur
+        (if (contains? #{\{ \( \[ \<} (first in))
+          (concat [(first in)] open)
+          (rest open))
+        (rest in)
+        ))))
+
+(->> (slurp "./in")
+     (clojure.string/split-lines)
+     (mapv vec)
+     (mapv check-line)
+     (filter some?)
+     (mapv (partial reduce #(+ (* 5 %1) (to-score %2)) 0))
+     (sort)
+     (#(get (vec %) (quot (count %) 2)))
+     (println)
+     )
+