Fix day10pt1 and add correct day10pt2
authorJack Kinsey <j.jameskinsey@gmail.com>
Wed, 11 Dec 2019 05:38:30 +0000 (00:38 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Wed, 11 Dec 2019 05:38:30 +0000 (00:38 -0500)
resources/day10 [new file with mode: 0644]
src/adventofcode2019/day10.clj
src/adventofcode2019/lib.clj

diff --git a/resources/day10 b/resources/day10
new file mode 100644 (file)
index 0000000..30f4f74
--- /dev/null
@@ -0,0 +1,33 @@
+..#..###....#####....###........#
+.##.##...#.#.......#......##....#
+#..#..##.#..###...##....#......##
+..####...#..##...####.#.......#.#
+...#.#.....##...#.####.#.###.#..#
+#..#..##.#.#.####.#.###.#.##.....
+#.##...##.....##.#......#.....##.
+.#..##.##.#..#....#...#...#...##.
+.#..#.....###.#..##.###.##.......
+.##...#..#####.#.#......####.....
+..##.#.#.#.###..#...#.#..##.#....
+.....#....#....##.####....#......
+.#..##.#.........#..#......###..#
+#.##....#.#..#.#....#.###...#....
+.##...##..#.#.#...###..#.#.#..###
+.#..##..##...##...#.#.#...#..#.#.
+.#..#..##.##...###.##.#......#...
+...#.....###.....#....#..#....#..
+.#...###..#......#.##.#...#.####.
+....#.##...##.#...#........#.#...
+..#.##....#..#.......##.##.....#.
+.#.#....###.#.#.#.#.#............
+#....####.##....#..###.##.#.#..#.
+......##....#.#.#...#...#..#.....
+...#.#..####.##.#.........###..##
+.......#....#.##.......#.#.###...
+...#..#.#.........#...###......#.
+.#.##.#.#.#.#........#.#.##..#...
+.......#.##.#...........#..#.#...
+.####....##..#..##.#.##.##..##...
+.#.#..###.#..#...#....#.###.#..#.
+............#...#...#.......#.#..
+.........###.#.....#..##..#.##...
index 2ea2fd23734afd2dc03b28c862b688aa0f8e4780..b12712d3bad56be2c8f76843dd921497ef7f4acc 100644 (file)
@@ -1,8 +1,51 @@
 (ns adventofcode2019.day10
     [:require [adventofcode2019.lib :refer :all]
-              [clojure.string :as str]
               [clojure.core.match :refer [match]]
-              [clojure.math.combinatorics :as combo]])
+              [clojure.set :as set]])
+
+(defn to-sightline [[a-x a-y] [b-x b-y]] 
+  (let [dy (- a-y b-y)
+        dx (- a-x b-x)
+        slope (if (zero? dx) :inf (rationalize (/ dy dx)))] 
+    [(neg? dy) (neg? dx) slope]))
+
+(defn order-sightlines [left right]
+  (let [set-order #(match [%1 %2 %3] 
+                          [false false :inf] [1 0]
+                          [false true     r] [2 r]
+                          [false true     0] [3 0]
+                          [true  true     r] [4 r]
+                          [true  false :inf] [5 0]
+                          [true  false    r] [6 r]
+                          [false false    0] [7 0]
+                          [false false    r] [8 r])]
+    (compare (apply set-order left) (apply set-order right))))
+
+(defn find-sightlines [center asteroids]
+  (map (partial to-sightline center) 
+       (disj asteroids center)))
+
+(defn order-vaporized [s-map]
+  (let [ordered-keys (sort order-sightlines (keys s-map))
+        ordered-vals (map s-map ordered-keys)
+        sorted-set-nth (fn [n s]
+                         (if (zero? n)
+                           (first s)
+                           (recur (dec n) (rest s))))
+        get-all-nths (fn [sets n]
+                       (map (partial sorted-set-nth n) sets))]
+    (->> (map (partial get-all-nths ordered-vals) (range))
+         (take-while (partial some some?))
+         (reduce concat)
+         (remove nil?))))
+
+(defn list-by-sightline [center asteroids]
+  (let [order-asts-in-lines #(compare (manhattan-distance center %1) 
+                                      (manhattan-distance center %2))] 
+    (reduce (partial merge-with set/union) 
+            (map #(hash-map %1 (sorted-set-by order-asts-in-lines %2)) 
+                 (find-sightlines center asteroids) 
+                 (disj asteroids center)))))
 
 (defn day10 []
   (let [input (get-list-from-file (input-file))
                        (map-indexed to-points)
                        (reduce concat)
                        (filter some?)
-                       (set))
-        to-slope (fn [[a-x a-y] [b-x b-y]] 
-                   (if (= a-x b-x) 
-                       :inf
-                       (rationalize 
-                        (/ (- a-y b-y)
-                           (- a-x b-x)))))
-        find-visible (fn [ast]
-                       (count (set (map (partial to-slope ast) 
-                                        (disj asteroids ast)))))
-        visible-count (map #(vector % (find-visible %)) asteroids)] 
-    (part1 (reduce (partial max-key second) visible-count))
-    #_(part2)))
+                       (into (sorted-set)))
+        [mv-ast mv-count] (reduce (partial max-key second) 
+                                  (map #(vector % (count (set (find-sightlines % asteroids)))) 
+                                       asteroids))
+        [x200 y200] (nth (order-vaporized (list-by-sightline mv-ast asteroids)) 199)] 
+    (part1 mv-count)
+    (part2 (+ (* 100 x200) y200))))
index 2699439f649085f7769e1b9154858ebd8e09f7d9..d992475caf866b9d7f992668c6bba59ec390ab53 100644 (file)
   (let [bottom-ns (last (str/split (str *ns*) #"\."))]
     (str "resources/" bottom-ns)))
 
+(defn manhattan-distance [[ax ay] [bx by]] 
+  (+ (Math/abs (- ax bx))
+     (Math/abs (- ay by))))
+
 (def part1 
   #(println (str "Part 1: " %)))
 (def part2