do day 8
This commit is contained in:
parent
4e548d4d40
commit
4c101f2afa
58
2025/src/day08.clj
Normal file
58
2025/src/day08.clj
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
(ns day08
|
||||||
|
(:require input-manager clojure.set
|
||||||
|
[clojure.string :as str]))
|
||||||
|
|
||||||
|
(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))))
|
||||||
|
|
||||||
|
(->> closest-points
|
||||||
|
(take (count input))
|
||||||
|
(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 *))
|
||||||
|
|
||||||
|
(->> (loop [circuits circuits
|
||||||
|
[[a b] & rst] (take 10000 closest-points)]
|
||||||
|
(let [circuit-a (first (filter #(% a) circuits))
|
||||||
|
circuit-b (first (filter #(% b) circuits))]
|
||||||
|
(if (= circuit-a circuit-b)
|
||||||
|
(recur circuits rst)
|
||||||
|
(let [combined (clojure.set/union circuit-a circuit-b)
|
||||||
|
circuits' (-> circuits
|
||||||
|
(disj circuit-a)
|
||||||
|
(disj circuit-b)
|
||||||
|
(conj combined))]
|
||||||
|
(if (= 1 (count circuits'))
|
||||||
|
[a b]
|
||||||
|
(recur circuits' rst))))))
|
||||||
|
(map first)
|
||||||
|
(apply *))
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user