Compare commits

..

No commits in common. "37c2786336d9b90a660c1a90e912154cb296e140" and "b0bbb1fcdbd24d9788b56ebcc769f03aa2ab35b9" have entirely different histories.

View File

@ -21,31 +21,30 @@
last last
(apply area)) (apply area))
;; wikipedia maths
(defn normalize [x] (min 1 (max -1 x))) (defn normalize [x] (min 1 (max -1 x)))
(defn orientation (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 A is to the left or right (or on) BC"
[[ax ay] [bx by] [cx cy]] [[ax ay] [bx by] [cx cy]]
(normalize (- (* (- by ay) (- cx bx)) (normalize (- (* (- by ay) (- cx bx))
(* (- bx ax) (- cy by))))) (* (- bx ax) (- cy by)))))
(defn intersect? (defn intersect? [[ax ay] [bx by]]
"Lines AB and CD intersect if points A and B falls on different sides of CD (let [o1 (orientation ax ay bx)
and if C and D fall on different sides of AB" o2 (orientation ax ay by)
[[a b] [c d]] o3 (orientation bx by ax)
(let [o1 (orientation a b c) o4 (orientation bx by ay)]
o2 (orientation a b d)
o3 (orientation c d a)
o4 (orientation c d b)]
(and (not= o1 o2) (not= o3 o4)))) (and (not= o1 o2) (not= o3 o4))))
;; part 2 ;; part 2
(def line-segments (map vector input (conj (vec (rest input)) (first input)))) (def line-segments (map vector input (conj (vec (rest input)) (first input))))
(->> furthest-points (->> furthest-points
(filter (fn [[a b]] (filter (fn [[a b]]
(let [segments (filter (fn [[seg-a seg-b]] (and (not= a seg-a) (let [segments (filter (fn [[seg-a seg-b]]
(not= a seg-b) (and (not= a seg-a)
(not= b seg-a) (not= a seg-b)
(not= b seg-b))) (not= b seg-a)
(not= b seg-b)))
line-segments) line-segments)
[[cx cy] [dx dy]] [a b]] [[cx cy] [dx dy]] [a b]]
(and (not (some #(intersect? [a b] %) segments)) (and (not (some #(intersect? [a b] %) segments))