mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 07:03:19 -09:00
it was math all along....
This commit is contained in:
parent
f60fc29a23
commit
125891900b
@ -4,58 +4,44 @@
|
|||||||
[input-manager :refer [get-input-raw]]))
|
[input-manager :refer [get-input-raw]]))
|
||||||
|
|
||||||
(def input
|
(def input
|
||||||
(->> (get-input-raw 13)
|
(->> (get-input-raw 2024 13)
|
||||||
c/split-on-double-newlines
|
c/split-on-double-newlines
|
||||||
(map #(c/get-match-groups #"(\d+)" %))
|
(map #(c/get-match-groups #"(\d+)" %))
|
||||||
(c/mmap first)
|
(c/mmap first)
|
||||||
(c/mmap parse-long)
|
(c/mmap parse-long)
|
||||||
(map #(map vector [:ax :ay :bx :by :px :py] %))
|
(map #(map vector [:ax :ay :bx :by :px :py] %))
|
||||||
(map #(into {} %))
|
(map #(into {} %))))
|
||||||
;
|
|
||||||
))
|
|
||||||
|
|
||||||
(defn push [m k vs]
|
|
||||||
(if (empty? vs)
|
|
||||||
m
|
|
||||||
(update m k #(if (nil? %1)
|
|
||||||
(set vs)
|
|
||||||
(into %1 %2))
|
|
||||||
vs)))
|
|
||||||
|
|
||||||
(defn get-cost [{:keys [ax ay bx by px py]}]
|
(defn get-cost [{:keys [ax ay bx by px py]}]
|
||||||
(loop [costs (sorted-map 0 #{[0 0]})
|
;; systems of equations. had to get out a pen and paper to work out a and b ;P
|
||||||
i 0]
|
;; px = ax * a + bx * b
|
||||||
(let [[cost locs] (first costs)
|
;; py = ay * a + by * b
|
||||||
costs' (dissoc costs cost)]
|
(let [b (/ (- (* ax py)
|
||||||
(if (or (nil? cost)
|
(* ay px))
|
||||||
(> i 1000)
|
(- (* ax by)
|
||||||
(some (fn [[x y]]
|
(* bx ay)))
|
||||||
(and (= x px)
|
a (/ (- px (* b bx))
|
||||||
(= y py)))
|
ax)]
|
||||||
locs))
|
(when (= 0.0
|
||||||
costs
|
(mod a 1.0)
|
||||||
(recur (-> costs'
|
(mod b 1.0))
|
||||||
(push (+ cost 3)
|
(+ (* 3 a) b))))
|
||||||
(->> 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))))))
|
|
||||||
|
|
||||||
|
(comment
|
||||||
;; part 1
|
;; part 1
|
||||||
(->> input
|
(->> input
|
||||||
(map get-cost)
|
(map get-cost)
|
||||||
(filter identity)
|
(filter identity)
|
||||||
(reduce +))
|
(filter identity)
|
||||||
|
(reduce +)
|
||||||
|
;
|
||||||
|
)
|
||||||
|
|
||||||
|
(def offset 10000000000000)
|
||||||
|
(->> input
|
||||||
|
|
||||||
|
(map #(update % :px + offset))
|
||||||
|
(map #(update % :py + offset))
|
||||||
|
(map get-cost)
|
||||||
|
(filter identity)
|
||||||
|
(reduce +)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user