57 lines
1.9 KiB
Clojure
57 lines
1.9 KiB
Clojure
(ns day08
|
|
(:require input-manager clojure.set))
|
|
|
|
(def input (->> (input-manager/get-input 2025 8)
|
|
(mapv (comp (partial mapv parse-long)
|
|
(partial re-seq #"\d+")))))
|
|
(def circuits (into #{} (map hash-set input)))
|
|
|
|
(defn square [x] (* x x))
|
|
(defn dist-squared [[ax ay az] [bx by bz]]
|
|
(+ (square (- ax bx))
|
|
(square (- ay by))
|
|
(square (- az bz))))
|
|
(def closest-points (->> (for [a input
|
|
b input
|
|
:when (not= a b)]
|
|
#{a b})
|
|
distinct
|
|
(map vec)
|
|
(sort-by (partial apply dist-squared))))
|
|
|
|
;; part 1
|
|
(->> closest-points
|
|
(take 1000)
|
|
(reduce (fn [circuits [a b]]
|
|
(let [circuit-a (first (filter #(% a) circuits))
|
|
circuit-b (first (filter #(% b) circuits))]
|
|
(if (= circuit-a circuit-b)
|
|
circuits
|
|
(let [combined (clojure.set/union circuit-a circuit-b)]
|
|
(-> circuits
|
|
(disj circuit-a)
|
|
(disj circuit-b)
|
|
(conj combined))))))
|
|
circuits)
|
|
(sort-by count)
|
|
(take-last 3)
|
|
(map count)
|
|
(apply *))
|
|
|
|
;; part 2
|
|
(->> (take 6498 closest-points)
|
|
(reduce (fn [circuits [a b]]
|
|
(let [circuit-a (first (filter #(% a) circuits))
|
|
circuit-b (first (filter #(% b) circuits))]
|
|
(if (= circuit-a circuit-b)
|
|
circuits
|
|
(let [combined (clojure.set/union circuit-a circuit-b)
|
|
circuits' (-> circuits (disj circuit-a) (disj circuit-b) (conj combined))]
|
|
(if (= 1 (count circuits'))
|
|
(reduced [a b])
|
|
circuits')))))
|
|
circuits)
|
|
(map first)
|
|
(apply *))
|
|
|