-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathday19.clj
More file actions
76 lines (57 loc) · 1.85 KB
/
day19.clj
File metadata and controls
76 lines (57 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(ns day19
(:require aoc))
(defn- perms [[a b c]]
[[a b c] [b c a] [c a b]
[c b (- a)] [b a (- c)] [a c (- b)]
[a (- b) (- c)] [b (- c) (- a)] [c (- a) (- b)]
[c (- b) a] [b (- a) c] [a (- c) b]
[(- a) b (- c)] [(- b) c (- a)] [(- c) a (- b)]
[(- c) b a] [(- b) a c] [(- a) c b]
[(- a) (- b) c] [(- b) (- c) a] [(- c) (- a) b]
[(- c) (- b) (- a)] [(- b) (- a) (- c)] [(- a) (- c) (- b)]])
(defn- all-rotations [beacon]
(aoc/transpose (map perms beacon)))
(defn- translate [beacon diff]
(mapv #(aoc/pt-3d+ % diff) beacon))
(defn- count-common [a b]
(let [pts (into (set a) b)]
(- (+ (count a) (count b))
(count pts))))
(defn- find-diff [a b]
(some
(fn [[i j]]
(let [diff (aoc/pt-3d- i j)
b+diff (translate b diff)]
(when (>= (count-common a b+diff) 12)
[diff b+diff])))
(for [i a , j b] [i j])))
(defn- rotate-scanner [a b]
(some #(find-diff a %) (all-rotations b)))
(defn- rotate-all-scanners [scanners]
(loop [[scanner & scanners'] scanners
acc [(first scanners)]
ds []]
(if (nil? scanner)
[acc ds]
(if-let [[d rotated-scanner] (some #(rotate-scanner % scanner) acc)]
(recur scanners' (conj acc rotated-scanner) (conj ds d))
(recur (conj (vec scanners') scanner) acc ds)))))
(defn- count-beams [scanners]
(count
(reduce
(fn [acc scanner]
(into acc scanner))
#{}
scanners)))
(defn- find-largest-distance [distances]
(reduce max (for [a distances
b distances]
(aoc/manhattan-3d a b))))
(defn solve [filename]
(let [data (-> filename
aoc/read-input
(aoc/parse-paragraphs :ints)
(->> (map rest)))
[beams distances] (rotate-all-scanners data)]
[(count-beams beams) (find-largest-distance distances)]))
(solve 19)