about summary refs log tree commit diff
path: root/day10.clj
blob: b838b0d0352b70462ea2ff1cdc7460a6aab6136e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(ns org.vuxu.aoc2021.day10
  (:require [clojure.string :as str]))

(defn fix [f x]
  (loop [x x]
    (let [fx (f x)]
      (if (= x fx) x (recur fx)))))

(let [[incomplete corrupted]
      (->> (slurp "day10")
           (str/split-lines)
           (map (partial fix #(str/replace % #"\(\)|\[\]|\{\}|<>" "")))
           ((juxt filter remove) (partial re-find #"[)}>\]]")))]
  (def incomplete incomplete)
  (def corrupted corrupted))

(def part1
  (->> incomplete
       (map (partial some #{\) \] \} \>}))
       (map {\) 3 \] 57 \} 1197 \> 25137})
       (apply +)))
;; => 216297

(def part2
  (->> corrupted
       (map reverse)
       (map (partial map {\( 1 \[ 2 \{ 3 \< 4}))
       (map (partial reduce #(+ (* %1 5) %2) 0))
       sort
       (#(nth % (bit-shift-right (count corrupted) 1)))))
;; => 2165057169