reduce superiority

This commit is contained in:
Adam Jeniski 2025-12-06 05:36:39 -10:00
parent 2ef3eb8d2c
commit c4cafb19b0

View File

@ -5,9 +5,7 @@
(def num-line? (comp not #{\* \+} first)) (def num-line? (comp not #{\* \+} first))
(def nums-raw (take-while num-line? input)) (def nums-raw (take-while num-line? input))
(def nums (map (comp (partial map parse-long) (def nums (map (comp (partial map parse-long) #(re-seq #"\d+" %)) nums-raw))
#(re-seq #"\d+" %))
nums-raw))
(def ops (->> input (def ops (->> input
(drop-while num-line?) (drop-while num-line?)
(first) (first)
@ -22,22 +20,21 @@
(apply +)) (apply +))
;; part 2 ;; part 2
(def MAX_LINE_IDX (dec (count (first nums-raw)))) (->> (range (count (first nums-raw)))
(loop [col-idx 0 (reduce (fn [{:keys [op-idx curr-nums] :as acc} col-idx]
op-idx 0 (let [op (get ops op-idx)
curr-nums [] col (->> nums-raw
acc 0] (map #(.charAt % col-idx))
(let [op (get ops op-idx) (filter (partial not= \space))
ans (+ acc (apply op curr-nums))] (map #(- (int %) (int \0))))]
(if (> col-idx MAX_LINE_IDX) (if (empty? col)
ans (-> acc
(let [col (->> nums-raw (update :ans + (apply op curr-nums))
(map #(.charAt % col-idx)) (update :op-idx inc)
(filter (partial not= \space)) (assoc :curr-nums []))
(map #(- (int %) (int \0))))] (update acc :curr-nums conj (parse-long (apply str col))))))
(if (empty? col) {:ans 0, :op-idx 0, :curr-nums []})
(recur (inc col-idx) (inc op-idx) [] ans) ((juxt :ans
(recur (inc col-idx) #(apply (last ops) (:curr-nums %))))
op-idx (apply +))
(conj curr-nums (parse-long (apply str col)))
acc))))))