day9: document part 2

master
Clyne 3 years ago
parent 7a62286670
commit 894df68d23

@ -5,60 +5,50 @@
(mapv (partial mapv #(- (int %) 48))) (mapv (partial mapv #(- (int %) 48)))
)) ))
(defn basin-bottom-single? [a b] (defn get-adj
(if (or (nil? a) (nil? b) (< a b)) 1 0)) "Gets vector of coords surrounding (x, y)."
[y x] [[(dec y) x] [(inc y) x] [y (dec x)] [y (inc x)]])
(defn basin-continues? [a b] (defn basin-continues?
"Determines if `b` is a continuation of the basin that includes `a`."
[a b]
(and (some? b) (not= b 9) (> b a))) (and (some? b) (not= b 9) (> b a)))
(defn basin-bottom? [hmap p adj] (defn basin-bottom?
(= 4 "Determines if point `p` in `hmap` is the bottom of a basin surrounded by
(apply + points `adj`."
(map [hmap p adj]
#(basin-bottom-single? (empty?
(get-in hmap p) (filter
(get-in hmap %)) #(let [q (get-in hmap %)] (and (some? q) (> (get-in hmap p) q)))
adj adj
)))) )))
(defn get-adj [y x] (defn find-basin
[[(dec y) x] "If point `yx` in `hmap` is in a basin (determined using `adj`), return a
[(inc y) x] list of points in the basin that are above `yx`."
[y (dec x)] [hmap yx adj]
[y (inc x)]]) (let [res (filter #(basin-continues? (get-in hmap yx)
(get-in hmap %))
(defn find-basin [hmap y x adj] adj)]
(let [res (reduce (if-not (empty? res)
#(if
(basin-continues? (get-in hmap [y x])
(get-in hmap %2))
(conj %1 %2)
%1)
[]
adj
)]
(if (empty? res)
[]
(apply (apply
(partial concat res) (partial concat res)
(map (map
#(find-basin hmap (first %) (second %) (apply get-adj %)) #(find-basin hmap % (apply get-adj %))
res)) res
) )))))
)
)
(println (->> (for [y (range 0 (count input-map))
(apply * x (range 0 (count (first input-map)))]
(take 3 (let [adj (get-adj y x)]
(sort > (when (basin-bottom? input-map [y x] adj)
(filter (inc (count (distinct (find-basin input-map [y x] adj))))
pos? )))
(for [y (range 0 (count input-map)) (filter some?)
x (range 0 (count (first input-map)))] (sort >)
(let [adj (get-adj y x)] (take 3)
(if (basin-bottom? input-map [y x] adj) (apply *)
(count (distinct (concat [[y x]] (find-basin input-map y x adj)))) (println)
0 )
))))))))

Loading…
Cancel
Save