Skip to content

Commit

Permalink
Day 13: Solve part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nihas101 committed Dec 13, 2023
1 parent 7b0c6a8 commit 0fadc20
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
60 changes: 57 additions & 3 deletions src/advent_of_code_2023/day13.clj
Original file line number Diff line number Diff line change
@@ -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])
(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)))
28 changes: 19 additions & 9 deletions test/advent_of_code_2023/day13_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))
(is (= 35915 (day13-2 input)))))

0 comments on commit 0fadc20

Please sign in to comment.