-
Notifications
You must be signed in to change notification settings - Fork 2
/
6.clj
118 lines (103 loc) · 3.4 KB
/
6.clj
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(ns advent-of-code.2018.6
(:require [advent-of-code.elves :refer :all]
[clojure.string :as cs]))
(def coords
(->> (day->input-lines 2018 6)
(map #(map parse-long (cs/split % #", ")))))
(defn dist* [[x1 y1] [x2 y2]]
(+ (Math/abs ^Integer (- x1 x2))
(Math/abs ^Integer (- y1 y2))))
(def dist (memoize dist*))
(def coord-bounds
[(apply (juxt min max) (map first coords))
(apply (juxt min max) (map second coords))])
(def cells
(let [[[min-x max-x] [min-y max-y]] coord-bounds]
(for [x (range min-x max-x), y (range min-y max-y)]
[x y])))
(def outliers ;; coordinates with infinite regions
(let [[[_min-x max-x] [_min-y max-y]] coord-bounds]
(->> (concat
(for [x (range max-x)] [x 0])
(for [y (range max-y)] [0 y])
(for [x (range max-x)] [x max-y])
(for [y (range max-y)] [max-x y]))
(map #(first (sort-by (partial dist %) coords)))
(set))))
(def markers
(pmap
(fn [cell]
(let [[closest next-closest] (sort-by (partial dist cell) coords)
tie? (= (dist cell closest) (dist cell next-closest))]
(if tie? '. closest)))
cells))
;; solve part 1
(->> outliers
(apply dissoc (frequencies markers))
(apply max-key second)
(second))
;; solve part 2
(->> cells
(pmap (fn [cell]
(< (apply + (map #(dist cell %) coords)) 10000)))
(filter true?)
(count))
;; viz
(comment
(require '[quil.core :as q]
'[quil.middleware :as m])
(def cells-with-closest
(pmap
(fn [cell]
(let [[closest next-closest] (sort-by (partial dist cell) coords)
tie? (= (dist cell closest) (dist cell next-closest))]
[cell (when-not tie? closest)]))
(let [[[min-x max-x] [min-y max-y]] coord-bounds]
(for [x (range (+ min-x max-x))
y (range (+ min-y max-y))]
[x y]))))
(def size
[(apply max (map ffirst cells-with-closest))
(apply max (map (comp second first) cells-with-closest))])
(def center (map #(/ % 2) size))
(def all-colors
(zipmap (sort-by #(dist % center) coords)
(rest (take-nth (int (/ 16581375 (count coords)))
(for [r (range 255) g (range 255) b (range 255)] [r g b])))))
(defn draw-state [[coords->color cells]]
(doseq [[[x y] color] cells]
(q/set-pixel x y color))
(q/no-stroke)
(doseq [[x y] coords
:let [color (coords->color [x y])
rgb [(q/red color)
(q/green color)
(q/blue color)]]]
(if (outliers [x y])
(apply q/fill (map #(min 255 (- % 92)) rgb))
(apply q/fill (map #(max 0 (+ % 92)) rgb)))
(q/ellipse x y 4 4)))
(defn setup []
(q/background 0)
(q/frame-rate 5)
(let [coords->color
(reduce-kv
(fn [m coord rgb]
(assoc m coord (apply q/color
(if (outliers coord)
(map #(min 255 (+ % 64)) rgb)
rgb))))
{}
all-colors)]
[coords->color
(keep (fn [[cell closest]]
(when closest
[cell (coords->color closest)]))
cells-with-closest)]))
(q/defsketch clock
:title "Chronal Coordinates"
:size size
:setup setup
:draw draw-state
:middleware [m/fun-mode]
:features [:keep-on-top]))