This commit is contained in:
Adam Jeniski 2025-12-09 06:33:04 -10:00
parent d2e6f920f3
commit a2492b2b2a

View File

@ -6,7 +6,7 @@
(defn area [[ax ay] [bx by]]
(* (inc (abs (- ax bx)))
(inc (abs (- ay by)))))
(def largest-squares (->> (for [a input
(def largest-rects (->> (for [a input
b input
:when (not= a b)]
#{a b})
@ -14,13 +14,13 @@
(map vec)
(sort-by #(apply area %))))
;; part 1
(->> largest-squares
(->> largest-rects
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"
"returns a value indicating if point B is to the left, right, or on the line AC"
[[ax ay] [bx by] [cx cy]]
(normalize (- (* (- by ay) (- cx bx))
(* (- bx ax) (- cy by)))))
@ -36,15 +36,16 @@
(def line-segments (map vector input (rest input)))
;; part 2
(->> largest-squares
(filter (fn [[a b]]
(let [segments (filter (fn overlapping? [[seg-a seg-b]]
(and (not= a seg-a) (not= a seg-b)
(not= b seg-a) (not= b seg-b))) ;; filter out segments that include points a or b
(->> largest-rects
(filter (fn [[a b :as rect]]
(let [segments (filter (fn overlapping? [[c d]]
(and (not= a c) (not= a d)
(not= b c) (not= b d))) ;; filter out segments that include points a or b
line-segments)
[[cx cy] [dx dy]] [a b]]
(and (not (some #(intersect? [a b] %) segments))
(not (some #(intersect? [[cx dy] [dx cy]] %) segments)))))) ;; check reverse diaganol too
[[cx cy] [dx dy]] rect
reverse-diag [[cx dy] [dx cy]]]
(and (not (some #(intersect? rect %) segments))
(not (some #(intersect? reverse-diag %) segments))))))
(map (partial apply area))
sort
last)