This commit is contained in:
Adam Jeniski 2023-12-04 01:19:38 -05:00
parent 5366c4c09e
commit b38a9393d3

View File

@ -8,17 +8,16 @@
(def lines (get-puzzle-input 4)) (def lines (get-puzzle-input 4))
(defn parse-nums [nums] (defn parse-lines [lines]
(->> (string/split nums #" ") (->> lines
(map #(rest (re-find #"^Card\s*\d+: ([\s\d]+) \| ([\s\d]+)" %)))
(map (partial map #(->> (string/split % #" ")
(filter not-empty) (filter not-empty)
(map #(Integer/parseInt %)) (map (fn [x] (Integer/parseInt x)))
(into #{}))) (into #{}))))))
;; part 1 ;; part 1
(->> lines (->> (parse-lines lines)
(map #(re-find #"^Card\s*\d+: ([\s\d]+) \| ([\s\d]+)" %))
(map rest)
(map (partial map parse-nums))
(map (partial apply intersection)) (map (partial apply intersection))
(map count) (map count)
(map #(math/pow 2 (dec %))) (map #(math/pow 2 (dec %)))
@ -27,23 +26,15 @@
;; part 2 ;; part 2
(loop [acc 0 (loop [acc 0
data (->> lines data (->> (parse-lines lines)
(map #(re-find #"^Card\s*\d+: ([\s\d]+) \| ([\s\d]+)" %)) (map #(vector 1 (count (apply intersection %))))
(map rest)
(map (fn [[win actual]]
[1 (count (intersection (parse-nums win)
(parse-nums actual)))]))
(into []))] (into []))]
(if (not-empty data) (if (not-empty data)
(let [cnt-to-add (second (first data)) (let [[card-cnt num-matches] (first data)]
curr-cnt (ffirst data)] (recur (+ acc card-cnt)
(concat (->> (rest data)
(recur (take num-matches)
(+ acc curr-cnt) (map #(update % 0 + card-cnt)))
(concat (->> data (drop (inc num-matches) data))))
rest
(take cnt-to-add)
(map #(update % 0 + curr-cnt)))
(drop (inc cnt-to-add) data))))
acc)) acc))