Day 2, Day 3, and a helper, add ASCII art day headings
This commit is contained in:
parent
527b1d41ea
commit
2246504b1a
112
2025.clj
112
2025.clj
@ -3,6 +3,22 @@
|
||||
|
||||
(defn get-input [day] (slurp (str "2025/day" day "-input.txt")))
|
||||
|
||||
(defn remove-index
|
||||
"Removes an item at the specified position in a vector"
|
||||
[coll ^long index]
|
||||
(-> (into (subvec coll 0 index)
|
||||
(subvec coll (inc index)))
|
||||
(with-meta (meta coll))))
|
||||
|
||||
;; _____ __
|
||||
;; | __ \ /_ |
|
||||
;; | | | | __ _ _ _ | |
|
||||
;; | | | |/ _` | | | | | |
|
||||
;; | |__| | (_| | |_| | | |
|
||||
;; |_____/ \__,_|\__, | |_|
|
||||
;; __/ |
|
||||
;; |___/
|
||||
|
||||
(def day1-input-test
|
||||
"L68
|
||||
L30
|
||||
@ -50,3 +66,99 @@ L82")
|
||||
(day1-p2 day1-input-test)
|
||||
(day1-p2 (get-input 1))
|
||||
:-)
|
||||
|
||||
;; _____ ___
|
||||
;; | __ \ |__ \
|
||||
;; | | | | __ _ _ _ ) |
|
||||
;; | | | |/ _` | | | | / /
|
||||
;; | |__| | (_| | |_| | / /_
|
||||
;; |_____/ \__,_|\__, | |____|
|
||||
;; __/ |
|
||||
;; |___/
|
||||
|
||||
(def day2-input-test
|
||||
"11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124")
|
||||
|
||||
(defn day2-parse-input [input]
|
||||
(->> (str/split (str/trim input) #",")
|
||||
(map #(str/split % #"-"))
|
||||
(map #(mapv parse-long %))
|
||||
(map (fn [[lo hi]] (range lo (inc hi))))
|
||||
flatten))
|
||||
|
||||
(defn day2-p1 [input]
|
||||
(->> input
|
||||
(keep #(let [num-str (str %)
|
||||
length (count num-str)
|
||||
half (/ length 2)]
|
||||
(when (and (even? length)
|
||||
(= (subs num-str 0 half)
|
||||
(subs num-str half length)))
|
||||
%)))
|
||||
(reduce +)))
|
||||
|
||||
(defn day2-p2 [input]
|
||||
(->> input
|
||||
(keep #(let [num-str (str %)
|
||||
length (count num-str)]
|
||||
(loop [p-size (quot length 2)]
|
||||
(cond
|
||||
(zero? p-size)
|
||||
nil
|
||||
(apply = (partition-all p-size num-str))
|
||||
%
|
||||
:else
|
||||
(recur (dec p-size))))))
|
||||
(reduce +)))
|
||||
|
||||
(comment
|
||||
(day2-p1 (day2-parse-input day2-input-test))
|
||||
(day2-p1 (day2-parse-input (get-input 2)))
|
||||
|
||||
(day2-p2 (day2-parse-input day2-input-test))
|
||||
(day2-p2 (day2-parse-input (get-input 2)))
|
||||
:-)
|
||||
|
||||
;; _____ ____
|
||||
;; | __ \ |___ \
|
||||
;; | | | | __ _ _ _ __) |
|
||||
;; | | | |/ _` | | | | |__ <
|
||||
;; | |__| | (_| | |_| | ___) |
|
||||
;; |_____/ \__,_|\__, | |____/
|
||||
;; __/ |
|
||||
;; |___/
|
||||
|
||||
(def day3-input-test
|
||||
"987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111")
|
||||
|
||||
(defn day3-get-max-battery [total-digits bank]
|
||||
(loop [remaining (dec total-digits)
|
||||
curr-idx 0
|
||||
nums []]
|
||||
(if (= (count nums) total-digits)
|
||||
(reduce (fn [acc digit] (+ (* acc 10) digit)) 0 nums)
|
||||
(let [subbank (subvec bank curr-idx (- (count bank) remaining))
|
||||
[idx max-num] (->> (map-indexed vector subbank)
|
||||
(reduce (fn [[idx max] [i n]]
|
||||
(if (> n max)
|
||||
[i n]
|
||||
[idx max]))
|
||||
[-1 -1]))]
|
||||
(recur (dec remaining) (+ curr-idx idx 1) (conj nums max-num))))))
|
||||
|
||||
(defn day3 [total-digits input]
|
||||
(->> (str/split-lines input)
|
||||
(mapv #(mapv (fn [c] (Character/digit c 10)) (vec %)))
|
||||
(map (partial day3-get-max-battery total-digits))
|
||||
taptap
|
||||
(reduce +)))
|
||||
|
||||
(comment
|
||||
(day3 2 day3-input-test)
|
||||
(day3 2 (get-input 3))
|
||||
(day3 12 day3-input-test)
|
||||
(day3 12 (get-input 3))
|
||||
:-)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user