Advertisement
luorduz

advent-22-16

Dec 24th, 2022
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clojure 2.49 KB | Source Code | 0 0
  1. (defn calc-total [total graph opened]
  2.   (apply + total (map #(get-in graph [% :rate]) opened))
  3. )
  4.  
  5. (defn can-open? [graph working node]
  6.   (and
  7.     (some? (working (:key node)))
  8.     (nil? ((:opened node) (:key node)))))
  9.  
  10. (defn get-neighbors [node working graph to-open]
  11.   (cond
  12.     to-open [(update node :opened conj (:key node))]
  13.     (= (:opened node) working) [node]
  14.     :else (->> node
  15.                :key graph :neigh
  16.                (map #(assoc {} :key % :opened (:opened node)))
  17.                (filter #(not ((:visited node) %)))
  18.                seq
  19.                (#(or % [node])))))
  20.  
  21. (defn clock [graph working paths]
  22.   (for
  23.     [node paths
  24.      toopen (if (can-open? graph working node) [false true] [false])
  25.      neighbor (get-neighbors node working graph toopen)]
  26.     (assoc neighbor
  27.            :total (calc-total (:total node) graph (:opened node))
  28.            :visited (conj (:visited node) (dissoc node :total :visited)))))
  29.  
  30. (with-open [rdr (clojure.java.io/reader "paths.in")]
  31.   (let [graph (->> rdr
  32.                 line-seq
  33.                 (map #(clojure.string/replace % "Valve " ""))
  34.                 (map #(clojure.string/replace % "has flow rate=" ""))
  35.                 (map #(clojure.string/replace % "; tunnels lead to valves" ""))
  36.                 (map #(clojure.string/replace % "; tunnel leads to valves" ""))
  37.                 (map #(clojure.string/replace % "; tunnel leads to valve" ""))
  38.                 (map #(clojure.string/replace % ", " "-"))
  39.                 (map #(clojure.string/split % #" "))
  40.                 (map (fn [[id rate neighbors]]
  41.                        {:key id
  42.                         :rate (Integer/parseInt rate)
  43.                         :neigh (set (clojure.string/split neighbors #"-"))}))
  44.                 (reduce #(assoc % (:key %2) %2) {}))
  45.         functional (->> graph vec (map second) (filter #(pos? (:rate %))) (map :key) set)]
  46.     (->> [{:key "AA" :opened #{} :total 0 :visited #{}}]
  47.       (iterate (partial clock graph functional))
  48.       (drop 30)
  49.       first
  50.       (map :total)
  51.       (apply max)
  52.       println)
  53.     (->> [{:key "AA" :opened #{} :total 0 :visited #{}}]
  54.       (iterate (partial clock graph functional))
  55.       (drop 26)
  56.       first
  57.       (map #(assoc {} (:opened %) (:total %)))
  58.       (reduce (fn [res step] (merge-with max res step)))
  59.       ((fn [totals]
  60.          (for [[popened ptotal] totals [eopened etotal] totals :when (not-any? popened eopened)]
  61.            (+ ptotal etotal))))
  62.       (apply max)
  63.       println)
  64. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement