advent-of-code/2025/src/day09.clj
2025-12-08 22:14:19 -10:00

56 lines
1.8 KiB
Clojure

(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))
(defn normalize [x] (min 1 (max -1 x)))
(defn orientation
"returns a value indicating if point A is to the left, right, or on the line BC"
[[ax ay] [bx by] [cx cy]]
(normalize (- (* (- by ay) (- cx bx))
(* (- bx ax) (- cy by)))))
(defn intersect?
"Lines AB and CD intersect if A and B falls on different sides of CD and if C and D fall on different sides of AB"
[[a b] [c d]]
(let [o1 (orientation a b c)
o2 (orientation a b d)
o3 (orientation c d a)
o4 (orientation c d b)]
(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))