Clean up day1

This commit is contained in:
Joseph Ferano 2025-12-01 19:33:42 +07:00
parent 79d521ffba
commit ab2f611f30

View File

@ -15,27 +15,13 @@ L99
R14 R14
L82") L82")
(defn day1-get-sequence [input] (defn day1 [input zero-count-fn]
(map (fn [line] (let [sequence (map (fn [line]
[(if (= "R" (str (first line))) [(if (= "R" (str (first line)))
#'+ #'+
#'-) #'-)
(Integer/parseInt (str/join (rest line)))]) (Integer/parseInt (str/join (rest line)))])
(str/split-lines input))) (str/split-lines input))]
(defn day1-p1 [input]
(let [sequence (day1-get-sequence input)]
(loop [s sequence
curr 50
zero-count 0]
(if (not (seq s))
zero-count
(let [[f amount] (first s)
new-dial-pos (mod (f curr amount) 100)]
(recur (rest s) new-dial-pos (if (zero? new-dial-pos) (inc zero-count) zero-count)))))))
(defn day1-p2 [input]
(let [sequence (day1-get-sequence input)]
(loop [s sequence (loop [s sequence
curr 50 curr 50
zero-count 0] zero-count 0]
@ -43,12 +29,21 @@ L82")
zero-count zero-count
(let [[f amount] (first s) (let [[f amount] (first s)
new-dial-pos (mod (f curr amount) 100) new-dial-pos (mod (f curr amount) 100)
spins (if (and (neg? (f amount)) spins (zero-count-fn curr (f amount) new-dial-pos)]
(not (zero? curr))) (println curr new-dial-pos amount spins)
(quot (+ (- 100 curr) amount) 100)
(quot (+ curr amount) 100))]
(recur (rest s) new-dial-pos (+ zero-count spins))))))) (recur (rest s) new-dial-pos (+ zero-count spins)))))))
(defn day1-p1 [input]
(day1 input (fn [_ _ new-dial-pos] (if (zero? new-dial-pos) 1 0))))
(defn day1-p2 [input]
(day1 input (fn [curr rotation _]
(let [amount (abs rotation)]
(if (and (neg? rotation)
(not (zero? curr)))
(quot (+ (- 100 curr) (abs amount)) 100)
(quot (+ curr amount) 100))))))
(comment (comment
(day1-p1 day1-input-test) (day1-p1 day1-input-test)
(day1-p1 (get-input 1)) (day1-p1 (get-input 1))