Advertisement
luorduz

advent-22-14

Dec 16th, 2022
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clojure 2.37 KB | Source Code | 0 0
  1. (defmulti get-in (fn [g & _] (:type g)))
  2.  
  3. (defmethod get-in :grid [grid [y x :as path]]
  4.   (cond
  5.     (> y (count (:val grid))) true
  6.     (or (= y (count (:val grid))) (>= x (-> grid :val first count)))
  7.       (clojure.core/get-in (:floor grid) path false)
  8.     :else (clojure.core/get-in (:val grid) path)))
  9.  
  10. (defmethod get-in :default [g p] (clojure.core/get-in g p))
  11.  
  12. (defmulti assoc-in (fn [g & _] (:type g)))
  13.  
  14. (defmethod assoc-in :grid [grid [y x :as path] value]
  15.   (if
  16.     (or (= y (count (:val grid))) (>= x (-> grid :val first count)))
  17.     (clojure.core/assoc-in grid (into [:floor] path) value)
  18.     (clojure.core/assoc-in grid (into [:val] path) value)))
  19.  
  20. (defmethod assoc-in :default [g p v] (clojure.core/assoc-in g p v))
  21.  
  22. (defn update-grid [grid [y x :as start]]
  23.   (cond
  24.     (nil? (get-in grid start)) nil
  25.     (not (true? (get-in grid [(inc y) x]))) (recur grid [(inc y) x])
  26.     (not (true? (get-in grid [(inc y) (dec x)]))) (recur grid [(inc y) (dec x)])
  27.     (not (true? (get-in grid [(inc y) (inc x)]))) (recur grid [(inc y) (inc x)])
  28.     (true? (get-in grid start)) nil
  29.     :else (assoc-in grid start true)
  30. ))
  31.  
  32. (defn tick [grid]
  33.   (loop [ticks 0 g grid]
  34.     (if
  35.       (nil? g) ticks
  36.       (recur (inc ticks) (update-grid g [0 500])))))
  37.  
  38. (defn add-rocks [grid steps]
  39.   (first (reduce
  40.     (fn [[grid [a b]] [c d]]
  41.       [(reduce
  42.         #(assoc-in %1 %2 true)
  43.         grid
  44.         (cond
  45.           (< a c) (map (fn [x] [b x]) (range a (inc c)))
  46.           (> a c) (map (fn [x] [b x]) (range c (inc a)))
  47.           (< b d) (map (fn [y] [y a]) (range b (inc d)))
  48.           (> b d) (map (fn [y] [y a]) (range d (inc b)))))
  49.        [c d]])
  50.     [grid (first steps)]
  51.     (rest steps))))
  52.  
  53. (with-open [rdr (clojure.java.io/reader "sand.in")]
  54.   (let
  55.     [steps
  56.      (->> rdr
  57.         line-seq
  58.         (map #(clojure.string/split % #" -> "))
  59.         (map flatten)
  60.         (map (fn [steps] (map #(clojure.string/split % #",") steps)))
  61.         (map flatten)
  62.         (map (fn [steps] (map #(Integer/parseInt %) steps)))
  63.         (map (partial partition 2)))
  64.      [maxx maxy] (->> steps flatten (partition 2) (apply map vector) (map #(apply max %)))
  65.      grid (vec (repeat (inc maxy) (vec (repeat (inc maxx) false))))
  66.     ]
  67.     (->> steps (reduce add-rocks grid) tick dec println)
  68.     (->> steps (reduce add-rocks {:type :grid, :val grid, :floor {}}) tick dec println)
  69. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement