1 (ns adventofcode2019.day18
2 [:require [adventofcode2019.lib :refer :all]
3 [adventofcode2019.intcode :as i]
4 [clojure.string :as str]
6 [clojure.core.match :refer [match]]
7 [clojure.math.combinatorics :as combo]
8 [clojure.data.priority-map :refer [priority-map-by]]])
10 (defn build-world [input]
11 (let [point-encode (->> input
18 passages (->> point-encode
19 (filter #(= \. (first %)))
22 interesting (->> point-encode
23 (filter #(and (not= \. (first %))
26 dkeys (select-keys interesting (filter #(Character/isLowerCase %)
28 doors (select-keys interesting (filter #(Character/isUpperCase %)
30 player (interesting \@)]
32 :t (apply conj passages player (vals dkeys))
36 (defn path-between [trav point-a point-b]
37 (let [succ (fn [sn [x y]]
38 (filter (every-pred trav (complement sn))
39 [[(inc x) y] [(dec x) y]
40 [x (inc y)] [x (dec y)]]))
41 update-openl (fn [ol sn pt ds]
43 (assoc m p [ds (manhattan-distance p point-b)]))
45 openl (priority-map-by (fn [[ag ah] [bg bh]]
46 (compare (+ ag ah) (+ bg bh))))]
47 (loop [openl (assoc openl point-a
48 [0 (manhattan-distance point-a point-b)])
51 (contains? openl point-b) (first (openl point-b))
53 :else (let [[point [dist _]] (peek openl)
54 seen (conj seen point)]
55 (recur (update-openl (pop openl) seen point (inc dist)) seen))))))
57 (defn accessible [{player :p dkeys :k trav :t}]
59 (mmap (partial path-between trav player))
60 (remove #(= ##Inf (second %)))
63 (defn acquire-all-keys [world]
64 (if (empty? (world :k)) 0
65 (let [accessible-keys (accessible world)
66 successor-world (fn [[kc ds]]
67 (let [kp (get-in world [:k kc])
68 dc (Character/toUpperCase kc)
69 dp (get-in world [:d dc])]
73 (update-in [:k] dissoc kc))]))
74 successors (map successor-world accessible-keys)
75 combine-levels (fn [[ds wo]] (+ ds (acquire-all-keys wo)))
76 ret (reduce min (map combine-levels successors))]
77 (clojure.pprint/pprint world)
78 (clojure.pprint/pprint ret)
82 (let [input (get-list-from-file (input-file))
83 world (build-world input)]
84 (part1 (acquire-all-keys world))