Add 2022 day 3
Diff
2022/3.clj | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
@@ -1,0 +1,50 @@
(require '[clojure.java.io :as io])
(require '[clojure.set :as set])
(defn char-to-number [char]
(cond
(Character/isLowerCase char) (- (int char) 96)
(Character/isUpperCase char) (- (int char) 38)))
(defn split-half [s]
(let [idx (quot (count s) 2)
[left right] (split-at idx s)]
[(set (apply str left)) (set (apply str right))]))
(defn part1-process-line [line]
(->> (split-half line)
(apply set/intersection)))
(defn part1 [lines]
(->> lines
(map part1-process-line)
(mapcat identity)
(map char-to-number)
(reduce +)))
(defn part2-process-lines [group]
(let [[g1 g2 g3] group]
(->> (set/intersection (set g1) (set g2) (set g3))
(map char-to-number)
(reduce +))))
(defn part2 [lines]
(->> (partition 3 lines)
(map part2-process-lines)
(reduce +)))
(def lines (with-open [rdr (io/reader *in*)] (vec (->> (line-seq rdr)))))
(println (part1 lines))
(println (part2 lines))