diff --git a/2024/src/core.clj b/2024/src/core.clj index 7139ba6..93eb232 100644 --- a/2024/src/core.clj +++ b/2024/src/core.clj @@ -54,3 +54,11 @@ "mapv mapv mapv f coll" [f coll] (mapv (partial mapv (partial mapv f)) coll)) + +(defn partition-by-counts [counts coll] + (->> counts + (reduce (fn [[acc coll] c] + (let [[a b] (split-at c coll)] + [(conj acc a) b])) + [[] coll]) + first)) diff --git a/2024/src/day09.clj b/2024/src/day09.clj index af5689d..e8975b8 100644 --- a/2024/src/day09.clj +++ b/2024/src/day09.clj @@ -3,20 +3,9 @@ [core :as c] [input-manager :refer [get-input]])) -(defn partition-by-counts [counts coll] - (loop [[c & cs] counts - acc [] - coll coll] - (cond - (nil? c) acc - :else (let [[a b] (split-at c coll)] - (recur cs (conj acc a) b))))) - -(def input (->> - ; (first (get-input 9)) - "2333133121414131402" - (map str) - (map parse-long))) +(def input (->> (first (get-input 9)) + (map str) + (map parse-long))) (def smol-nums (->> input (take-nth 2) (into []))) (def smol-spaces (->> input rest (take-nth 2) (into []))) @@ -24,7 +13,7 @@ ;; part 1 (let [forward (map-indexed #(repeat %2 %1) smol-nums)] (->> forward reverse flatten - (partition-by-counts smol-spaces) + (c/partition-by-counts smol-spaces) (interleave forward) flatten (take (reduce + smol-nums))