Advertisement
luorduz

advent-22-08

Dec 10th, 2022 (edited)
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Clojure 2.17 KB | Source Code | 0 0
  1. (defn inspect [tline vline] (
  2.   ->> (map vector tline vline)
  3.     (reduce
  4.       (fn [[highest line] [tree visibility]] [
  5.         (max tree highest)
  6.         (->> (> tree highest) (or visibility) (conj line))
  7.       ])
  8.       [-1 []]
  9.     )
  10.     second
  11. ))
  12.  
  13. (defn find-visible [dir tline] (
  14.   ->> tline
  15.     (map-indexed vector)
  16.     (reduce
  17.       (fn [line [ix step]] (
  18.         cond
  19.           (zero? ix) (conj line (assoc step dir 0))
  20.           (== 1 ix) (conj line (assoc step dir 1))
  21.           :else (
  22.             loop [prev (dec ix)] (
  23.               if (or (>= (get-in line [prev "val"]) (step "val")) (zero? prev))
  24.                 (conj line (assoc step dir (- ix prev)))
  25.                 (recur (- prev (get-in line [prev dir])))
  26.             ))))
  27.       []
  28.     )
  29. ))
  30.              
  31.  
  32. (with-open [rdr (clojure.java.io/reader "trees.in")] (
  33.   let [
  34.     matrix (->> rdr line-seq (map #(for [digit %] (-> digit str (Integer/parseInt)))))
  35.   ] (
  36.     ->> matrix
  37.       ((fn [matrix] [matrix (map #(map neg? %1) matrix)]))
  38.       ((fn [[tmatrix vmatrix]] [tmatrix (map inspect tmatrix vmatrix)]))
  39.       ((fn [[tmatrix vmatrix]] [(map reverse tmatrix) (map reverse vmatrix)]))
  40.       ((fn [[tmatrix vmatrix]] [tmatrix (map inspect tmatrix vmatrix)]))
  41.       ((fn [[tmatrix vmatrix]] [(apply map vector tmatrix) (apply map vector vmatrix)]))
  42.       ((fn [[tmatrix vmatrix]] [tmatrix (map inspect tmatrix vmatrix)]))
  43.       ((fn [[tmatrix vmatrix]] [(map reverse tmatrix) (map reverse vmatrix)]))
  44.       ((fn [[tmatrix vmatrix]] (map inspect tmatrix vmatrix)))
  45.       flatten
  46.       (filter true?)
  47.       count
  48.       println
  49.     )
  50.     (
  51.     ->> matrix
  52.       (#(map (fn [line] (vec (map (fn [tree] {"val" tree}) line))) %1))
  53.       ((fn [matrix] (map (partial find-visible "left") matrix)))
  54.       (#(map reverse %))
  55.       ((fn [matrix] (map (partial find-visible "right") matrix)))
  56.       (#(apply map vector %))
  57.       ((fn [matrix] (map (partial find-visible "up") matrix)))
  58.       (#(map reverse %))
  59.       ((fn [matrix] (map (partial find-visible "down") matrix)))
  60.       flatten
  61.       ((fn [trees] (map #(apply * (-> % (dissoc "val") vals)) trees)))
  62.       (apply max)
  63.       println
  64.     )
  65. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement