From 8afe06a504283d2408f1fe5cdd684e56d0fa945f Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Fri, 18 Dec 2020 12:12:32 +0100 Subject: day16 --- day16.clj | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 day16.clj (limited to 'day16.clj') diff --git a/day16.clj b/day16.clj new file mode 100644 index 0000000..fce1aa3 --- /dev/null +++ b/day16.clj @@ -0,0 +1,63 @@ +(let [data (slurp "day16") + [a b c] (clojure.string/split data #"\n\n")] + (def ranges a) + (def mine b) + (def nearby c)) + +(def line-ranges + (->> ranges + clojure.string/split-lines + (map (fn [line] + (->> line + (re-seq #"(\d+)-(\d+)") + (map (fn [[_ f t]] + (range (Integer/parseInt f) + (inc (Integer/parseInt t))))) + flatten))))) + +(def all-in-ranges + (->> line-ranges + flatten + set)) + +(def nearby-tickets + (->> nearby + clojure.string/split-lines + rest + (map (fn [s] (clojure.string/split s #","))) + (map (partial map #(Integer/parseInt %))))) + +(reduce + (remove all-in-range (flatten nearby-tickets))) ; => 24021 + +(def my-ticket + (->> mine + clojure.string/split-lines + second + (#(clojure.string/split % #",")) + (map #(Integer/parseInt %)) + vec)) + +(def valid-tickets + (->> nearby-tickets + (filter (fn [t] (empty? (remove all-in-range t) ))))) + +(defn valid-positions [line-range] + (let [s (set line-range)] + (filter (fn [i] + (every? (fn [ticket] + (s (nth ticket i))) + valid-tickets)) + (range (count line-ranges))))) + +(->> line-ranges + (map valid-positions) + (sort-by count) + (cons ()) + (partition 2 1) + (map (fn [[x y]] (first (remove (set x) y)))) + + (drop 6) ;; ??? + (take 6) + + (map my-ticket) + (apply *)) ; => 1289178686687 -- cgit 1.4.1