From 125891900bce271a22cfd564b2277358c9b0bc76 Mon Sep 17 00:00:00 2001 From: ajet Date: Fri, 29 Aug 2025 11:38:14 -0900 Subject: [PATCH] it was math all along.... --- 2024/src/day13.clj | 76 +++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/2024/src/day13.clj b/2024/src/day13.clj index f4eb7f6..153a662 100644 --- a/2024/src/day13.clj +++ b/2024/src/day13.clj @@ -4,58 +4,44 @@ [input-manager :refer [get-input-raw]])) (def input - (->> (get-input-raw 13) + (->> (get-input-raw 2024 13) c/split-on-double-newlines (map #(c/get-match-groups #"(\d+)" %)) (c/mmap first) (c/mmap parse-long) (map #(map vector [:ax :ay :bx :by :px :py] %)) - (map #(into {} %)) - ; - )) - -(defn push [m k vs] - (if (empty? vs) - m - (update m k #(if (nil? %1) - (set vs) - (into %1 %2)) - vs))) + (map #(into {} %)))) (defn get-cost [{:keys [ax ay bx by px py]}] - (loop [costs (sorted-map 0 #{[0 0]}) - i 0] - (let [[cost locs] (first costs) - costs' (dissoc costs cost)] - (if (or (nil? cost) - (> i 1000) - (some (fn [[x y]] - (and (= x px) - (= y py))) - locs)) - costs - (recur (-> costs' - (push (+ cost 3) - (->> locs - (map #(-> % - (update 0 + ax) - (update 1 + ay))) - (filter (fn [[x y]] - (and (<= x px) - (<= y py)))))) - (push (+ cost 1) - (->> locs - (map #(-> % - (update 0 + bx) - (update 1 + by))) - (filter (fn [[x y]] - (and (<= x px) - (<= y py))))))) - (inc i)))))) + ;; systems of equations. had to get out a pen and paper to work out a and b ;P + ;; px = ax * a + bx * b + ;; py = ay * a + by * b + (let [b (/ (- (* ax py) + (* ay px)) + (- (* ax by) + (* bx ay))) + a (/ (- px (* b bx)) + ax)] + (when (= 0.0 + (mod a 1.0) + (mod b 1.0)) + (+ (* 3 a) b)))) +(comment ;; part 1 -(->> input - (map get-cost) - (filter identity) - (reduce +)) + (->> input + (map get-cost) + (filter identity) + (filter identity) + (reduce +) + ; + ) + (def offset 10000000000000) + (->> input + + (map #(update % :px + offset)) + (map #(update % :py + offset)) + (map get-cost) + (filter identity) + (reduce +)))