Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn calc-total [total graph opened]
- (apply + total (map #(get-in graph [% :rate]) opened))
- )
- (defn can-open? [graph working node]
- (and
- (some? (working (:key node)))
- (nil? ((:opened node) (:key node)))))
- (defn get-neighbors [node working graph to-open]
- (cond
- to-open [(update node :opened conj (:key node))]
- (= (:opened node) working) [node]
- :else (->> node
- :key graph :neigh
- (map #(assoc {} :key % :opened (:opened node)))
- (filter #(not ((:visited node) %)))
- seq
- (#(or % [node])))))
- (defn clock [graph working paths]
- (for
- [node paths
- toopen (if (can-open? graph working node) [false true] [false])
- neighbor (get-neighbors node working graph toopen)]
- (assoc neighbor
- :total (calc-total (:total node) graph (:opened node))
- :visited (conj (:visited node) (dissoc node :total :visited)))))
- (with-open [rdr (clojure.java.io/reader "paths.in")]
- (let [graph (->> rdr
- line-seq
- (map #(clojure.string/replace % "Valve " ""))
- (map #(clojure.string/replace % "has flow rate=" ""))
- (map #(clojure.string/replace % "; tunnels lead to valves" ""))
- (map #(clojure.string/replace % "; tunnel leads to valves" ""))
- (map #(clojure.string/replace % "; tunnel leads to valve" ""))
- (map #(clojure.string/replace % ", " "-"))
- (map #(clojure.string/split % #" "))
- (map (fn [[id rate neighbors]]
- {:key id
- :rate (Integer/parseInt rate)
- :neigh (set (clojure.string/split neighbors #"-"))}))
- (reduce #(assoc % (:key %2) %2) {}))
- functional (->> graph vec (map second) (filter #(pos? (:rate %))) (map :key) set)]
- (->> [{:key "AA" :opened #{} :total 0 :visited #{}}]
- (iterate (partial clock graph functional))
- (drop 30)
- first
- (map :total)
- (apply max)
- println)
- (->> [{:key "AA" :opened #{} :total 0 :visited #{}}]
- (iterate (partial clock graph functional))
- (drop 26)
- first
- (map #(assoc {} (:opened %) (:total %)))
- (reduce (fn [res step] (merge-with max res step)))
- ((fn [totals]
- (for [[popened ptotal] totals [eopened etotal] totals :when (not-any? popened eopened)]
- (+ ptotal etotal))))
- (apply max)
- println)
- ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement