From d61d11562ec367db975ffbc55fc2a8c978e8fc9f Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 27 Dec 2025 10:47:21 +0700 Subject: [PATCH] Day 6 --- 2025.clj | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/2025.clj b/2025.clj index 367b8ed..2c4a332 100644 --- a/2025.clj +++ b/2025.clj @@ -12,6 +12,7 @@ (def directions {:N [-1 0] :S [1 0] :W [0 -1] :E [0 1] :NW [-1 -1] :NE [-1 1] :SW [1 -1] :SE [1 1]}) + ;; _____ __ ;; | __ \ /_ | ;; | | | | __ _ _ _ | | @@ -288,3 +289,62 @@ L82") (day5-p2 day5-input-test) (day5-p2 (get-input 5)) :-) + +;; _____ __ +;; | __ \ / / +;; | | | | __ _ _ _ / /_ +;; | | | |/ _` | | | | | '_ \ +;; | |__| | (_| | |_| | | (_) | +;; |_____/ \__,_|\__, | \___/ +;; __/ | +;; |___/ + +(def day6-input-test +"123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * + ") + +(defn day6-p1 [input] + (let [parsed (->> (str/split-lines input) + (mapv #(->> (str/split % #"\s+") + (remove empty?))))] + (->> (butlast parsed) + (mapv #(mapv parse-long %)) + (into [(mapv #(resolve (symbol %)) (last parsed))]) + (apply mapv vector) + (map #(apply (first %) (rest %))) + (reduce +)))) + +(defn day6-p2 [input] + (let [grid (->> (str/split-lines input) + (mapv vec)) + row-count (count grid)] + (loop [curr-col (dec (count (first grid))) + curr-row 0 + curr-num-str "" + nums [] + total 0] + (if (= curr-col -1) + total + (let [curr (get-in grid [curr-row curr-col]) + last-row (= curr-row (dec row-count))] + (cond + (Character/isDigit curr) + (recur curr-col (mod (inc curr-row) row-count) (str curr-num-str curr) nums total) + + last-row + (let [sum (parse-long curr-num-str)] + (if (#{\+ \*} curr) + (recur (dec curr-col) 0 "" [] (+ total (reduce (resolve (symbol (str curr))) (conj nums sum)))) + (recur (dec curr-col) 0 "" (if sum (conj nums sum) []) total))) + + :else + (recur curr-col (mod (inc curr-row) row-count) curr-num-str nums total))))))) + +(comment + (day6-p1 day6-input-test) ;; => 4277556 + (day6-p1 (get-input 6)) ;; => 6343365546996 + (day6-p2 day6-input-test) ;; => 3263827 + (day6-p2 (get-input 6)) ;; => 11136895955912 + :-)