mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 05:23:17 -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]]))
|
||||
|
||||
(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 +)))
|
||||
|
Loading…
x
Reference in New Issue
Block a user