about summary refs log tree commit diff
path: root/day09.clj
diff options
context:
space:
mode:
Diffstat (limited to 'day09.clj')
-rw-r--r--day09.clj53
1 files changed, 53 insertions, 0 deletions
diff --git a/day09.clj b/day09.clj
new file mode 100644
index 0000000..6066b53
--- /dev/null
+++ b/day09.clj
@@ -0,0 +1,53 @@
+(ns org.vuxu.aoc2021.day09
+  (:require [clojure.string :as str]))
+
+(defn parse-digit [char]
+  (Character/digit char 10))
+
+(def data
+  (->> (slurp "day09")
+       (str/split-lines)
+       (mapv (partial mapv parse-digit))))
+
+(def datamap
+  (zipmap (for [x (range (count data))
+                y (range (count (data x)))]
+            [x y])
+          (flatten data)))
+
+(def low-points 
+  (filter (fn [[x y]]
+            (< (get datamap [x y])
+               (apply min (for [[dx dy] [[-1 0] [1 0] [0 -1] [0 1]]]
+                            (get datamap [(+ x dx) (+ y dy)] 999)))))
+          (keys datamap)))
+
+(def part1
+  (->> low-points
+       (map datamap)
+       (map inc)
+       (apply +)))
+;; => 512
+
+(defn grow [[x y]]
+  (->> (for [[dx dy] [[-1 0] [1 0] [0 -1] [0 1]]]
+         [(+ x dx) (+ y dy)])
+       (filter (fn [[xx yy]]
+                 (not= 9 (get datamap [xx yy] 9))))))
+
+(defn fix [f x]
+  (loop [x x]
+    (let [fx (f x)]
+      (if (= x fx) x (recur fx)))))
+
+(defn measure [p]
+  (count (fix (fn [ps] (apply conj ps (mapcat grow ps)))
+              (set [p]))))
+
+(def part2
+  (->> low-points
+       (map measure)
+      sort
+      (take-last 3)
+      (apply *)))
+;; => 1600104