Skip to content

Commit 3aee00b

Browse files
committed
2024, day 8
1 parent 47faec9 commit 3aee00b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

2024/08.clj

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(require '[clojure.string :as str])
2+
3+
(def the-map (str/split-lines (slurp "2024/08.in")))
4+
5+
(defn in-bounds? [position] (every? #(<= 0 % (dec (count the-map))) position))
6+
(defn p*2 [[i j]] [(* 2 i) (* 2 j)])
7+
(defn p- [[i1 j1] [i2 j2]] [(- i1 i2) (- j1 j2)])
8+
9+
(def antenna-groups
10+
(reduce (fn [m [a i j]] (update m a (fnil conj []) [i j]))
11+
{}
12+
(for [i (range (count the-map)), j (range (count the-map))
13+
:let [antenna (get-in the-map [i j])]
14+
:when (not= \. antenna)]
15+
[antenna i j])))
16+
17+
(defn antinodes [antenna-group]
18+
(into [] (comp cat (filter in-bounds?))
19+
(for [a (range (count antenna-group)),
20+
b (range (inc a) (count antenna-group))
21+
:let [a (nth antenna-group a)
22+
b (nth antenna-group b)]]
23+
[(p- (p*2 a) b)
24+
(p- (p*2 b) a)])))
25+
26+
(count
27+
(into #{} cat
28+
(vals (update-vals antenna-groups antinodes))))
29+
30+
;; ---
31+
32+
(defn p*n [[i j] n] [(* n i) (* n j)])
33+
(defn p+ [[i1 j1] [i2 j2]] [(+ i1 i2) (+ j1 j2)])
34+
35+
(defn antinodes+harmonics [antenna-group]
36+
(into [] (comp cat (filter in-bounds?))
37+
(for [a (range (count antenna-group)),
38+
b (range (inc a) (count antenna-group))
39+
:let [a (nth antenna-group a)
40+
b (nth antenna-group b)]]
41+
(concat
42+
(take-while in-bounds? (map #(p+ a (p*n (p- b a) %)) (range)))
43+
(take-while in-bounds? (map #(p+ b (p*n (p- a b) %)) (range)))))))
44+
45+
(count
46+
(into #{} cat
47+
(vals (update-vals antenna-groups antinodes+harmonics))))

0 commit comments

Comments
 (0)