diff --git a/src/advent_of_code_2023/day13.clj b/src/advent_of_code_2023/day13.clj index 099e751..a21d409 100644 --- a/src/advent_of_code_2023/day13.clj +++ b/src/advent_of_code_2023/day13.clj @@ -1,9 +1,63 @@ (ns advent-of-code-2023.day13 (:require + [advent-of-code-2023.utils.graph :as g] [advent-of-code-2023.utils.string :as u])) -(defn parse-input [input]) +(defn parse-input [input] + (mapv #(g/parse-positional-map % identity) + (u/split-sections input))) -(defn day13-1 [parsed-input]) +(defn- mirrored?-fn [smudges] + (fn [a b] + (= + (count (filterv (fn [[aa bb]] (not= aa bb)) + (mapv vector (take-while (complement nil?) a) + (take-while (complement nil?) b)))) + smudges))) -(defn day13-2 [parsed-input]) \ No newline at end of file +(defn- vertical-mirror? [{:keys [positions ^long height ^long width]} + ^long x mirrored?] + (let [max-offset (min (inc x) (- width x))] + (mirrored? + (transduce + (mapcat (fn [^long o] (mapv #(positions [(- x o) %]) (range height)))) + conj [] (range (inc max-offset))) + (transduce + (mapcat (fn [^long o] (mapv #(positions [(+ (inc x) o) %]) (range height)))) + conj [] (range (inc max-offset)))))) + +(defn- horizontal-mirror? [{:keys [positions ^long height ^long width]} + ^long y mirrored?] + (let [max-offset (min (inc y) (- height y))] + (mirrored? + (transduce + (mapcat (fn [^long o] (mapv #(positions [% (- y o)]) (range width)))) + conj [] (range (inc max-offset))) + (transduce + (mapcat (fn [^long o] (mapv #(positions [% (+ (inc y) o)]) (range width)))) + conj [] (range (inc max-offset)))))) + +(defn- mirrors [{:keys [^long width ^long height] :as p} comp-fn] + (let [vertical-candidates + (filterv (fn [x] (vertical-mirror? p x comp-fn)) + (range (dec width))) + horizontal-candidates + (filterv (fn [y] (horizontal-mirror? p y comp-fn)) + (range (dec height)))] + {:vertical-candidates vertical-candidates + :horizontal-candidates horizontal-candidates})) + +(defn- day13 [mirror-landscape mirrored?] + (transduce + (comp + (map (fn [p] (mirrors p mirrored?))) + (map (fn [{:keys [vertical-candidates horizontal-candidates]}] + (+ (if-let [v (first vertical-candidates)] (inc ^long v) 0) + (* 100 (if-let [h (first horizontal-candidates)] (inc ^long h) 0)))))) + + mirror-landscape)) + +(defn day13-1 [mirror-landscape] + (day13 mirror-landscape (mirrored?-fn 0))) + +(defn day13-2 [mirror-landscape] + (day13 mirror-landscape (mirrored?-fn 1))) \ No newline at end of file diff --git a/test/advent_of_code_2023/day13_test.clj b/test/advent_of_code_2023/day13_test.clj index 4cfc81e..c02d041 100644 --- a/test/advent_of_code_2023/day13_test.clj +++ b/test/advent_of_code_2023/day13_test.clj @@ -4,26 +4,36 @@ [advent-of-code-2023.day13 :refer :all] [advent-of-code-2023.test-utils :as tu])) -(defonce ^:private example-input (parse-input "")) +(defonce ^:private example-input (parse-input "#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..#")) (def ^:private input (parse-input (tu/slurp-input "resources/day13.txt"))) (deftest day13-1-example-test (testing "day13-1 example" - (is (= nil - (day13-1 example-input))))) + (is (= 405 (day13-1 example-input))))) (deftest day13-1-test (testing "day13-1 example" - (is (= nil - (day13-1 input))))) + (is (= 36041 (day13-1 input))))) (deftest day13-2-example-test (testing "day13-2 example" - (is (= nil - (day13-2 example-input))))) + (is (= 400 (day13-2 example-input))))) (deftest day13-2-test (testing "day13-2 example" - (is (= nil - (day13-2 input))))) \ No newline at end of file + (is (= 35915 (day13-2 input))))) \ No newline at end of file