1 (ns adventofcode2019.day10
2 [:require [adventofcode2019.lib :refer :all]
3 [clojure.core.match :refer [match]]
4 [clojure.set :as set]])
6 (defn to-sightline [[a-x a-y] [b-x b-y]]
9 slope (if (zero? dx) :inf (rationalize (/ dy dx)))]
10 [(neg? dy) (neg? dx) slope]))
12 (defn order-sightlines [left right]
13 (let [set-order #(match [%1 %2 %3]
14 [false false :inf] [1 0]
18 [true false :inf] [5 0]
21 [false false r] [8 r])]
22 (compare (apply set-order left) (apply set-order right))))
24 (defn find-sightlines [center asteroids]
25 (map (partial to-sightline center)
26 (disj asteroids center)))
28 (defn order-vaporized [s-map]
29 (let [ordered-keys (sort order-sightlines (keys s-map))
30 ordered-vals (map s-map ordered-keys)
31 sorted-set-nth (fn [n s]
34 (recur (dec n) (rest s))))
35 get-all-nths (fn [sets n]
36 (map (partial sorted-set-nth n) sets))]
37 (->> (map (partial get-all-nths ordered-vals) (range))
38 (take-while (partial some some?))
42 (defn list-by-sightline [center asteroids]
43 (let [order-asts-in-lines #(compare (manhattan-distance center %1)
44 (manhattan-distance center %2))]
45 (reduce (partial merge-with set/union)
46 (map #(hash-map %1 (sorted-set-by order-asts-in-lines %2))
47 (find-sightlines center asteroids)
48 (disj asteroids center)))))
51 (let [input (get-list-from-file (input-file))
53 (map-indexed (fn [i m]
54 (if (= \# m) [i j] nil)) l))
56 (map-indexed to-points)
60 [mv-ast mv-count] (reduce (partial max-key second)
61 (map #(vector % (count (set (find-sightlines % asteroids))))
63 [x200 y200] (nth (order-vaporized (list-by-sightline mv-ast asteroids)) 199)]
65 (part2 (+ (* 100 x200) y200))))