From b0bbb1fcdbd24d9788b56ebcc769f03aa2ab35b9 Mon Sep 17 00:00:00 2001 From: ajet Date: Mon, 8 Dec 2025 21:42:38 -1000 Subject: [PATCH] do day 9 --- 2025/src/day09.clj | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2025/src/day09.clj diff --git a/2025/src/day09.clj b/2025/src/day09.clj new file mode 100644 index 0000000..d6e5fd4 --- /dev/null +++ b/2025/src/day09.clj @@ -0,0 +1,55 @@ +(ns day09 + (:require input-manager)) + +(def input (->> (input-manager/get-input 2025 9) + (mapv #(mapv parse-long (re-seq #"\d+" %))))) + +(defn area [[ax ay] [bx by]] + (* (inc (abs (- ax bx))) + (inc (abs (- ay by))))) +(def furthest-points + (->> (for [a input + b input + :when (not= a b)] + #{a b}) + distinct + (map vec) + (sort-by #(apply area %)))) + +;; part 1 +(->> furthest-points + last + (apply area)) + +;; wikipedia maths +(defn normalize [x] (min 1 (max -1 x))) +(defn orientation + "returns a value indicating if point A is to the left or right (or on) BC" + [[ax ay] [bx by] [cx cy]] + (normalize (- (* (- by ay) (- cx bx)) + (* (- bx ax) (- cy by))))) + +(defn intersect? [[ax ay] [bx by]] + (let [o1 (orientation ax ay bx) + o2 (orientation ax ay by) + o3 (orientation bx by ax) + o4 (orientation bx by ay)] + (and (not= o1 o2) (not= o3 o4)))) + +;; part 2 +(def line-segments (map vector input (conj (vec (rest input)) (first input)))) +(->> furthest-points + (filter (fn [[a b]] + (let [segments (filter (fn [[seg-a seg-b]] + (and (not= a seg-a) + (not= a seg-b) + (not= b seg-a) + (not= b seg-b))) + line-segments) + [[cx cy] [dx dy]] [a b]] + (and (not (some #(intersect? [a b] %) segments)) + (not (some #(intersect? [[cx dy] [dx cy]] %) segments)))))) + (sort-by (partial apply area)) + last + (apply area)) +