From f1a195aa5c74e264f1aa82aea8556d0501b5abf7 Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Wed, 11 Dec 2019 00:38:30 -0500 Subject: [PATCH] Fix day10pt1 and add correct day10pt2 --- resources/day10 | 33 +++++++++++++++++ src/adventofcode2019/day10.clj | 67 ++++++++++++++++++++++++++-------- src/adventofcode2019/lib.clj | 4 ++ 3 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 resources/day10 diff --git a/resources/day10 b/resources/day10 new file mode 100644 index 0000000..30f4f74 --- /dev/null +++ b/resources/day10 @@ -0,0 +1,33 @@ +..#..###....#####....###........# +.##.##...#.#.......#......##....# +#..#..##.#..###...##....#......## +..####...#..##...####.#.......#.# +...#.#.....##...#.####.#.###.#..# +#..#..##.#.#.####.#.###.#.##..... +#.##...##.....##.#......#.....##. +.#..##.##.#..#....#...#...#...##. +.#..#.....###.#..##.###.##....... +.##...#..#####.#.#......####..... +..##.#.#.#.###..#...#.#..##.#.... +.....#....#....##.####....#...... +.#..##.#.........#..#......###..# +#.##....#.#..#.#....#.###...#.... +.##...##..#.#.#...###..#.#.#..### +.#..##..##...##...#.#.#...#..#.#. +.#..#..##.##...###.##.#......#... +...#.....###.....#....#..#....#.. +.#...###..#......#.##.#...#.####. +....#.##...##.#...#........#.#... +..#.##....#..#.......##.##.....#. +.#.#....###.#.#.#.#.#............ +#....####.##....#..###.##.#.#..#. +......##....#.#.#...#...#..#..... +...#.#..####.##.#.........###..## +.......#....#.##.......#.#.###... +...#..#.#.........#...###......#. +.#.##.#.#.#.#........#.#.##..#... +.......#.##.#...........#..#.#... +.####....##..#..##.#.##.##..##... +.#.#..###.#..#...#....#.###.#..#. +............#...#...#.......#.#.. +.........###.#.....#..##..#.##... diff --git a/src/adventofcode2019/day10.clj b/src/adventofcode2019/day10.clj index 2ea2fd2..b12712d 100644 --- a/src/adventofcode2019/day10.clj +++ b/src/adventofcode2019/day10.clj @@ -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)) @@ -13,16 +56,10 @@ (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)))) diff --git a/src/adventofcode2019/lib.clj b/src/adventofcode2019/lib.clj index 2699439..d992475 100644 --- a/src/adventofcode2019/lib.clj +++ b/src/adventofcode2019/lib.clj @@ -16,6 +16,10 @@ (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 -- 2.26.2