do day 9
This commit is contained in:
parent
9e57a7dffb
commit
b0bbb1fcdb
55
2025/src/day09.clj
Normal file
55
2025/src/day09.clj
Normal file
@ -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))
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user