From a9c16e4b6b49f850817caa845f3263972ad3b237 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sun, 19 Dec 2021 09:40:37 -0500 Subject: [PATCH] day19: wip part 1 --- day19/part1.clj | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 day19/part1.clj diff --git a/day19/part1.clj b/day19/part1.clj new file mode 100644 index 0000000..25122da --- /dev/null +++ b/day19/part1.clj @@ -0,0 +1,72 @@ +(require '[clojure.core.reducers :as r]) +(require 'clojure.string) + +(defn build-scanner-reports [input-lines] + (loop [scanners [] rem-input input-lines] + (if (empty? rem-input) + scanners + (let [[this nxt] (split-with (comp not empty?) rem-input)] + (recur + (->> (rest this) + (map #(read-string (clojure.string/join ["[" % "]"]))) + (conj scanners)) + (rest nxt)))))) + +(defn all-orientations [[x y z]] + (map (fn [[i j k]] [(* x i) (* y j) (* z k)]) + '([1 1 1] [-1 1 1] [1 -1 1] [-1 -1 1] [1 1 -1] [-1 1 -1] [1 -1 -1] [-1 -1 -1]))) + +(defn group-orientations [lst] + (for [i (range 0 8)] (map #(nth % i) lst))) + +(defn scanner-orientations [scanner] + (group-orientations (map all-orientations scanner))) + +(defn add-coordinate [[a b c] [d e f]] [(+ a d) (+ b e) (+ c f)]) + +(defn add-coordinate-lists [l1 l2] + (for [i (range 0 (count l1))] + (for [j (range 0 (count l2))] + (add-coordinate (nth l1 i) (nth l2 j))))) + +(defn compare-scanners [s1 s2] + (let [o1 [s1];(scanner-orientations s1) + o2 (scanner-orientations s2)] + (for [o o1 p o2] + (add-coordinate-lists o p)))) + +(defn check-beacon-coord [scanner coord index] + (not (empty? (filter #(= coord %) (map #(nth % index) scanner))))) + +(defn beacon-coord-match? [req coord compared-scanners] + (let [cnt (count (first (first compared-scanners)))] + (< (dec req) + (reduce + (fn [tot n] + (cond-> tot + (and (< tot req) + (reduce + #(or %1 %2) + (map #(check-beacon-coord % coord n) compared-scanners))) + inc)) + 0 + (range 0 cnt))))) + +(->> (slurp "./in") + (clojure.string/split-lines) + (build-scanner-reports) + ((fn [scanners] + (for [s1i (range 0 (count scanners)) + s2 (drop (inc s1i) scanners) + :let [res (compare-scanners (nth scanners s1i) s2)]] + (let [cnt (count (first (first res)))] + (into '() + (r/fold + r/cat + (fn [final c] + (cond-> final + (beacon-coord-match? 4 c res) + (r/append! c))) + (into [] (distinct (nth (iterate (partial reduce concat) res) 2))))))))) + (println)) +