]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
add day 12
authorClyne Sullivan <clyne@bitgloo.com>
Sun, 12 Dec 2021 16:46:28 +0000 (11:46 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sun, 12 Dec 2021 16:46:28 +0000 (11:46 -0500)
day12/part1.clj [new file with mode: 0644]
day12/part2.clj [new file with mode: 0644]

diff --git a/day12/part1.clj b/day12/part1.clj
new file mode 100644 (file)
index 0000000..fe8ca7c
--- /dev/null
@@ -0,0 +1,35 @@
+(require '[clojure.string :as str])
+
+(def caves (->> (slurp "./in")
+           (str/split-lines)
+           (map #(str/split % #"-"))
+           (map (partial map str))
+           (#(concat % (map reverse %)))
+           ))
+
+(def search-caves
+  (memoize (fn [id]
+    (filter #(= (first %) id) caves))))
+
+(defn get-caves-forward [klst]
+  (let [end (first klst)]
+    (if (str/starts-with? end "end")
+      [klst]
+      (map #(cons (second %) klst)
+        (filter
+          (fn [cv]
+            (or (< (int (first (second cv))) 96)
+                (not-any? #(= % (second cv)) klst)))
+          (search-caves end)
+          )))))
+
+(loop [lst (get-caves-forward ["start"]) ms '()]
+  (let [nxt (->> lst
+                 (map get-caves-forward)
+                 (apply concat))
+        mtchs  (concat ms (filter #(= (first %) "end") nxt))
+        nxtlst (filter #(not= (first %) "end") nxt)]
+    (if (empty? nxtlst)
+      (println (count mtchs))
+      (recur nxtlst mtchs))))
+
diff --git a/day12/part2.clj b/day12/part2.clj
new file mode 100644 (file)
index 0000000..8a57ec1
--- /dev/null
@@ -0,0 +1,43 @@
+(require '[clojure.string :as str])
+
+(def caves (->> (slurp "./in")
+           (str/split-lines)
+           (map #(str/split % #"-"))
+           (map (partial map str))
+           (#(concat % (map reverse %)))
+           ))
+
+(def search-caves
+  (memoize (fn [id]
+    (filter #(= (first %) id) caves))))
+
+(defn get-caves-forward [klst]
+  (let [end (first klst)]
+    (if (str/starts-with? end "end")
+      [klst]
+      (map #(cons (second %) klst)
+        (filter
+          (fn [cv]
+            (and
+              (not (str/starts-with? (second cv) "start"))
+              (or
+                (< (int (first (second cv))) 96)
+                (let [rv (count (filter #(= % (second cv)) klst))
+                      lw (filter #(= (str/lower-case %) %) klst)]
+                  (or (= 0 rv) (< (- (count lw) (count (distinct lw))) 1)))
+                )
+              ))
+          (search-caves end)
+          )))))
+
+(loop [lst (get-caves-forward ["start"]) ms []]
+  (println (count ms))
+  (let [nxt (->> lst
+                 (map get-caves-forward)
+                 (apply concat))
+        mtchs (concat ms (filter #(= (first %) "end") nxt))
+        nxtlst (filter #(not= (first %) "end") nxt)]
+    (if (empty? nxtlst)
+      (println (count mtchs))
+      (recur nxtlst mtchs))))
+