diff --git a/deps.edn b/deps.edn index aa775c4..a57e713 100644 --- a/deps.edn +++ b/deps.edn @@ -1,3 +1,4 @@ {:paths ["src" "cookies"] :deps {org.clojure/clojure {:mvn/version "1.12.0"} - clj-http/clj-http {:mvn/version "3.13.0"}}} + clj-http/clj-http {:mvn/version "3.13.0"} + org.clojure/math.combinatorics {:mvn/version "0.3.0"}}} diff --git a/src/day8.clj b/src/day8.clj new file mode 100644 index 0000000..2d5ef91 --- /dev/null +++ b/src/day8.clj @@ -0,0 +1,80 @@ +(ns day8 + (:require [mapped-area] + [helpers :refer [get-input]] + [clojure.math.combinatorics :refer [combinations]])) + +(def test-city +"............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............") + +(def test-city2 +".......... +.......... +.......... +....a..... +........a. +.....a.... +.......... +.......... +.......... +..........") + +(defn antinodes [a [[x1 y1] [x2 y2]]] + (let [xd (- x1 x2) + yd (- y1 y2) + an1 [(+ x1 xd) (+ y1 yd)] + an2 [(- x2 xd) (- y2 yd)]] + (filter (partial mapped-area/mapped? a) [an1 an2]))) + +(defn antennas [a] + (reduce + (fn [m [coord value]] + (if (= value \.) + m + (update m value #(conj % coord)))) + {} + (mapped-area/mapped-area-indexed-seq a))) + +(defn antenna-pairs [a] + (mapcat (fn [[_ a]] (combinations a 2)) (antennas a))) + +(defn find-antinodes [m a-fn] + (let [a (mapped-area/read-mapped-area m)] + (->> (antenna-pairs a) + (mapcat #(a-fn a (vec %)))))) + +(println "Number of antinodes:" + (-> + (get-input 8) + (find-antinodes antinodes) + set + count)) + +(defn antinodes-resonant [a [[x1 y1] [x2 y2]]] + (let [xd (- x1 x2) + yd (- y1 y2) + nodes-fn + (fn [x y nodes op] + (if (not (mapped-area/mapped? a x y)) + nodes + (recur (op x xd) (op y yd) (conj nodes [x y]) op)))] + (concat + (nodes-fn x1 y1 [] +) + (nodes-fn x2 y2 [] -)))) + +(println "Number of antinodes:" + (-> + (get-input 8) + (find-antinodes antinodes-resonant) + set + count)) diff --git a/src/mapped_area.clj b/src/mapped_area.clj index 2efeb19..49bb087 100644 --- a/src/mapped_area.clj +++ b/src/mapped_area.clj @@ -71,3 +71,8 @@ ijkl") (test #'h) (test #'w) (test #'mapped?) + +(defn mapped-area-indexed-seq [a] + (for [x (range (w a)) + y (range (h a))] + (vector [x y] (get-in a [x y]))))