aboutsummaryrefslogtreecommitdiffstats
path: root/day12
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-12 11:46:28 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-12 11:46:28 -0500
commitc026fd3f3ec13552e959bd561f9f38ae9b729aa2 (patch)
tree7f3e2352ad61a9ad1846aa10dbdccd261551e9e3 /day12
parentbf8d9874947868de154b21244d7e6fb974b56e3f (diff)
add day 12
Diffstat (limited to 'day12')
-rw-r--r--day12/part1.clj35
-rw-r--r--day12/part2.clj43
2 files changed, 78 insertions, 0 deletions
diff --git a/day12/part1.clj b/day12/part1.clj
new file mode 100644
index 0000000..fe8ca7c
--- /dev/null
+++ b/day12/part1.clj
@@ -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
index 0000000..8a57ec1
--- /dev/null
+++ b/day12/part2.clj
@@ -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))))
+