]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day9: improved both parts
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Dec 2021 22:37:13 +0000 (17:37 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Dec 2021 22:37:13 +0000 (17:37 -0500)
day9/part1.clj
day9/part2.clj

index 813d5a6f8c30bc2e09fae6a84939306e069c1eef..7bd8e234e856387e819dafcda5e28e38f6bff860 100644 (file)
@@ -5,33 +5,17 @@
        (mapv (partial mapv #(- (int %) 48)))
        ))
 
-(defn compare-heights [a b]
-  (if (or (nil? a) (nil? b) (< a b)) 1 0))
+(defn get-adj [y x]
+  (map (partial get-in input-map)
+       [[(dec y) x] [(inc y) x] [y (dec x)] [y (inc x)]]))
 
-(println
-  (apply +
-      (for [y (range 0 (count input-map))
-            x (range 0 (count (first input-map)))]
-        (let [height (get-in input-map [y x])]
-          (if
-            (= 4
-               (apply +
-                 (map
-                   (partial compare-heights height)
-                   [
-                    (get-in input-map [(dec y) x])
-                    (get-in input-map [(inc y) x])
-                    (get-in input-map [y (dec x)])
-                    (get-in input-map [y (inc x)])
-                   ]
-                   )
-                 )
-               )
-            (inc height)
-            0
-            )
-          )
-        )
-    )
-  )
+(->> (for [y (range 0 (count input-map))
+           x (range 0 (count (first input-map)))
+           :let [height (get-in input-map [y x])]
+           :when (every? #(or (nil? %) (< height %))
+                         (get-adj y x))]
+           (inc height))
+     (apply +)
+     (println)
+     )
 
index dd05b7d1a66b9cae73675af7ff9456bcb44c7510..3030c4e03145b624a2e88e6e5f127177fef15b34 100644 (file)
@@ -1,9 +1,4 @@
-(def input-map
-  (->> (slurp "./in")
-       (clojure.string/split-lines)
-       (mapv vec)
-       (mapv (partial mapv #(- (int %) 48)))
-       ))
+(require '[clojure.set])
 
 (defn get-adj 
   "Gets vector of coords surrounding (x, y)."
 
 (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)))
+  [a b] (and (some? b) (not= b 9) (> b a)))
 
 (defn basin-bottom?
   "Determines if point `p` in `hmap` is the bottom of a basin surrounded by
    points `adj`."
   [hmap p adj]
-  (empty?
-    (filter
-      #(let [q (get-in hmap %)] (and (some? q) (> (get-in hmap p) q)))
-      adj
-      )))
+  (every?
+    #(let [q (get-in hmap %)] (or (nil? q) (< (get-in hmap p) q)))
+    adj
+    ))
 
 (defn find-basin
   "If point `yx` in `hmap` is in a basin (determined using `adj`), return a
   (let [res (filter #(basin-continues? (get-in hmap yx)
                                        (get-in hmap %))
                     adj)]
-    (if-not (empty? res)
-      (apply
-        (partial concat res)
-        (map
-          #(find-basin hmap % (apply get-adj %))
-          res
-          )))))
-
-(->> (for [y (range 0 (count input-map))
-           x (range 0 (count (first input-map)))]
-       (let [adj (get-adj y x)]
-         (when (basin-bottom? input-map [y x] adj)
-           (inc (count (distinct (find-basin input-map [y x] adj))))
+    (apply (partial clojure.set/union res)
+           (map #(set (find-basin hmap % (apply get-adj %))) res)
            )))
-     (filter some?)
+
+(->> (slurp "./in")
+     (clojure.string/split-lines)
+     (mapv vec)
+     (mapv (partial mapv #(- (int %) 48)))
+     (#(for [y (range 0 (count %))
+             x (range 0 (count (first %)))
+             :let [adj (get-adj y x)]
+             :when (basin-bottom? % [y x] adj)]
+         (inc (count (find-basin % [y x] adj)))))
      (sort >)
      (take 3)
      (apply *)