day12: document part2, make part1 more concise
parent
c026fd3f3e
commit
c507675805
@ -1,43 +1,48 @@
|
|||||||
(require '[clojure.string :as str])
|
(require '[clojure.string :as str])
|
||||||
|
|
||||||
(def caves (->> (slurp "./in")
|
; Build list of cave connections:
|
||||||
|
(def caves (->> (slurp "./in")
|
||||||
(str/split-lines)
|
(str/split-lines)
|
||||||
(map #(str/split % #"-"))
|
(map #(str/split % #"-"))
|
||||||
(map (partial map str))
|
(map (partial map str))
|
||||||
(#(concat % (map reverse %)))
|
(#(concat % (map reverse %)))
|
||||||
))
|
))
|
||||||
|
|
||||||
(def search-caves
|
; Try to help execution speed by memoizing cave searches:
|
||||||
|
(def search-caves
|
||||||
(memoize (fn [id]
|
(memoize (fn [id]
|
||||||
(filter #(= (first %) id) caves))))
|
(filter #(= (first %) id) caves))))
|
||||||
|
|
||||||
(defn get-caves-forward [klst]
|
; Given current path 'path', return a list of valid paths that branch
|
||||||
(let [end (first klst)]
|
; from 'path'. Note, paths are stored in reverse order.
|
||||||
(if (str/starts-with? end "end")
|
(defn get-caves-forward [path]
|
||||||
[klst]
|
(map #(cons (second %) path)
|
||||||
(map #(cons (second %) klst)
|
|
||||||
(filter
|
(filter
|
||||||
(fn [cv]
|
(fn [cv]
|
||||||
(and
|
(and
|
||||||
|
; Do not return to start
|
||||||
(not (str/starts-with? (second cv) "start"))
|
(not (str/starts-with? (second cv) "start"))
|
||||||
(or
|
(or
|
||||||
|
; Always allow uppercase paths
|
||||||
(< (int (first (second cv))) 96)
|
(< (int (first (second cv))) 96)
|
||||||
(let [rv (count (filter #(= % (second cv)) klst))
|
(let [lw (filter #(= (str/lower-case %) %) path)]
|
||||||
lw (filter #(= (str/lower-case %) %) klst)]
|
(or
|
||||||
(or (= 0 rv) (< (- (count lw) (count (distinct lw))) 1)))
|
; Allow lowercase paths that have never been visited
|
||||||
)
|
(= 0 (count (filter #(= % (second cv)) path)))
|
||||||
))
|
; Allow one duplicate if there are none yet
|
||||||
(search-caves end)
|
(= 0 (- (count lw) (count (distinct lw)))))))))
|
||||||
)))))
|
; Only work with paths that we connect to
|
||||||
|
(search-caves (first path))
|
||||||
|
)))
|
||||||
|
|
||||||
(loop [lst (get-caves-forward ["start"]) ms []]
|
(loop [paths (get-caves-forward ["start"]) complete-paths []]
|
||||||
(println (count ms))
|
(let [branches (->> paths
|
||||||
(let [nxt (->> lst
|
|
||||||
(map get-caves-forward)
|
(map get-caves-forward)
|
||||||
(apply concat))
|
(apply concat))
|
||||||
mtchs (concat ms (filter #(= (first %) "end") nxt))
|
complete-branches (concat complete-paths
|
||||||
nxtlst (filter #(not= (first %) "end") nxt)]
|
(filter #(= (first %) "end") branches))
|
||||||
(if (empty? nxtlst)
|
next-paths (filter #(not= (first %) "end") branches)]
|
||||||
(println (count mtchs))
|
(if (empty? next-paths)
|
||||||
(recur nxtlst mtchs))))
|
(println (count complete-branches))
|
||||||
|
(recur next-paths complete-branches))))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue