This commit is contained in:
Joseph Ferano 2025-12-27 10:47:21 +07:00
parent 70483bbfc1
commit d61d11562e

View File

@ -12,6 +12,7 @@
(def directions {:N [-1 0] :S [1 0] :W [0 -1] :E [0 1] (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]}) :NW [-1 -1] :NE [-1 1] :SW [1 -1] :SE [1 1]})
;; _____ __ ;; _____ __
;; | __ \ /_ | ;; | __ \ /_ |
;; | | | | __ _ _ _ | | ;; | | | | __ _ _ _ | |
@ -288,3 +289,62 @@ L82")
(day5-p2 day5-input-test) (day5-p2 day5-input-test)
(day5-p2 (get-input 5)) (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
:-)