This commit is contained in:
Adam Jeniski 2023-12-03 13:46:54 -05:00
parent 34a17e5edb
commit 656d8bb725

View File

@ -24,32 +24,33 @@
matches))) matches)))
(mapcat identity))) (mapcat identity)))
(defn coords-to-check [row col num-str]
(for [r [(dec row) row (inc row)]
c (range (dec col) (+ col (count num-str) 1))]
[r c]))
;; part 1 ;; part 1
(->> (parse-nums lines) (->> (parse-nums lines)
(filter (fn touches-symbol? [[row-idx col-idx s]] (filter (fn touches-symbol? [[row col s]]
(let [length (count s)] (->> (coords-to-check row col s)
(->> (for [r [(dec row-idx) row-idx (inc row-idx)] (map char-map)
c (range (dec col-idx) (+ col-idx length 1))] (filter (comp not nil?))
(char-map [r c])) (some #(not (or (Character/isDigit %) (= % \.)))))))
(filter (comp not nil?))
(some #(not (or (Character/isDigit %) (= % \.))))))))
(map #(Integer/parseInt (nth % 2))) (map #(Integer/parseInt (nth % 2)))
(reduce +)) (reduce +))
;; stars is a list of [[star-row-idx star-col-idx] "num"] ;; stars is a list of [[star-row-idx star-col-idx] "num"]
;; stars is list of each * char found touching a number when iterating by nums ;; stars is list of each * char found touching a number when iterating by nums
(def stars (->> (parse-nums lines) (def stars (->> (parse-nums lines)
(map (fn touching-stars [[row-idx col-idx s]] (map (fn touching-stars [[row col s]]
(let [length (count s)] (->> (coords-to-check row col s)
(->> (for [r [(dec row-idx) row-idx (inc row-idx)] (map #(vector % (char-map %)))
c (range (dec col-idx) (+ col-idx length 1))] (filter #(not (nil? (second %))))
[[r c] (char-map [r c])]) (filter #(= \* (second %)))
(filter #(not (nil? (second %)))) (map #(vector % s)))))
(filter #(= \* (second %)))
(map #(vector % s))))))
(filter seq) (filter seq)
(mapcat identity) (mapcat identity)
(map #(vector (ffirst %) (second %))))) (map (fn [[[coords] num]] [coords num]))))
;; gears is a set of [star-row-idx star-col-idx] ;; gears is a set of [star-row-idx star-col-idx]
;; gears is a list of each * char that is touching exactly 2 nums ;; gears is a list of each * char that is touching exactly 2 nums
@ -67,6 +68,5 @@
(map #(second (update % 1 (partial map second)))) (map #(second (update % 1 (partial map second))))
(map (partial map #(Integer/parseInt %))) (map (partial map #(Integer/parseInt %)))
(map (partial reduce *)) (map (partial reduce *))
(reduce +) (reduce +))
)