2022 Day 3 Clojure

This commit is contained in:
Luke Hubmayer-Werner 2022-12-03 20:56:27 +10:30
parent 24520cb3b6
commit d07a16bbf9
1 changed files with 38 additions and 0 deletions

38
2022/day3.clj Normal file
View File

@ -0,0 +1,38 @@
(require '[clojure.string :as str])
(require '[clojure.set])
(require '[clojure.zip :as zip])
(def elves (str/split-lines (slurp "input/3")))
(defn priority [char]
(let [
c (int char)
a (int \a)
z (int \z)
A (int \A)
]
(if (<= a c z)
(+ (- c a) 1)
(+ (- c A) 27))))
; Part 1
(defn two-knapsacks [line] (
let [
half (/ (count line) 2)
[knapsack-1 knapsack-2] (map set (split-at half line))
]
(priority (first (clojure.set/intersection knapsack-1 knapsack-2)
))))
(println (reduce + (map two-knapsacks elves)))
; Part 2
(defn chunker [sequence num]
(if (<= (count sequence) num)
[sequence]
(let [[head tail] (split-at num sequence)] (lazy-seq (cons head (chunker tail num))))))
(defn three-elves [lines] (
let [
sets (map set lines)
common (apply clojure.set/intersection sets)
]
(priority (first common))))
(println (reduce + (map three-elves (chunker elves 3))))