diff --git a/src/day7.clj b/src/day7.clj index 1327d06..1febf42 100644 --- a/src/day7.clj +++ b/src/day7.clj @@ -48,8 +48,6 @@ (recur (set (propagate beams))) (println "Beam split" @split "times"))) -(def memo (atom {})) - (def timelines (memoize (fn [[l c]] diff --git a/src/day8.clj b/src/day8.clj new file mode 100644 index 0000000..24026c6 --- /dev/null +++ b/src/day8.clj @@ -0,0 +1,87 @@ +(ns day8 + (:require + [clojure.edn :as edn] + [clojure.math :as math] + [clojure.string :as string] + [helpers :refer [get-input]])) + +(def example +"162,817,812 +57,618,57 +906,360,560 +592,479,940 +352,342,300 +466,668,158 +542,29,236 +431,825,988 +739,650,466 +52,470,668 +216,146,977 +819,987,18 +117,168,530 +805,96,715 +346,949,466 +970,615,88 +941,993,340 +862,61,35 +984,92,344 +425,690,689") + +;(def input (->> (string/split-lines example) +; (map #(string/split % #",")) +; (map (partial map edn/read-string)))) + +(def input (->> (string/split-lines (get-input 8)) + (map #(string/split % #",")) + (map (partial map edn/read-string)))) + +(def d + (memoize + (fn [a b] + (->> + (map - a b) + (map #(* % %)) + (reduce +) + math/sqrt)))) + +(def distances + (->> + (mapcat (fn [a] (mapcat (fn [b] [[a b] (d a b)]) input)) input) + (apply hash-map) + (remove (comp zero? val)) + (sort-by val))) + +(defn connect! [connections [a b]] + (let [ca (first (filter #(some % [a]) connections)) + cb (first (filter #(some % [b]) connections)) + c' (remove #(some % [a b]) connections)] + (cond + (and (empty? ca) (not-empty cb)) + (conj c' (conj cb a)) + (and (not-empty ca) (empty? cb)) + (conj c' (conj ca b)) + (and (not-empty ca) (not-empty cb)) + (conj c' (into ca cb)) + :else + (conj connections #{a b})))) + +(loop [connections [] + ;; doubling number of connections to skip dealing with double distances betweer A -> B and B -> A + ;;distances (take 20 distances)] + distances (take 2000 distances)] + (if (empty? distances) + (println "Result:" + (->> (sort-by count > connections) + (take 3) + (map count) + (reduce * 1))) + (recur (connect! connections (key (first distances))) (rest (rest distances))))) + +(loop [connections [] + distances distances] + (let [pair (key (first distances)) + connections' (connect! connections pair)] + ;;(if (= 20 (count (first connections'))) + (if (= 1000 (count (first connections'))) + (println "Pair:" pair, "multiplying X coordinates:" (apply * (map first pair))) + (recur connections' (rest (rest distances))))))