refactor, add more comments

This commit is contained in:
Adam Jeniski 2023-12-03 13:22:46 -05:00
parent d6a9747af2
commit 34a17e5edb

View File

@ -4,6 +4,8 @@
[core :refer [get-puzzle-input re-seq-pos]]))
(def lines (get-puzzle-input 3))
;; a hash-map of [row-idx col-idx] to char
(def char-map
(->> lines
(map-indexed (fn [row-idx line]
@ -34,10 +36,9 @@
(map #(Integer/parseInt (nth % 2)))
(reduce +))
;; part 2
;; 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
(let [stars (->> (parse-nums lines)
(def stars (->> (parse-nums lines)
(map (fn touching-stars [[row-idx col-idx s]]
(let [length (count s)]
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
@ -48,22 +49,24 @@
(map #(vector % s))))))
(filter seq)
(mapcat identity)
(map #(vector (ffirst %) (second %))))
(map #(vector (ffirst %) (second %)))))
;; 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 (->> stars
;; gears is a set of [star-row-idx star-col-idx]
;; gears is a list of each * char that is touching exactly 2 nums
(def gears (->> stars
(map first)
(frequencies)
(filter #(= (second %) 2))
(map first)
(into #{}))]
(->> stars
(into #{})))
;; part 2
(->> stars
(group-by first)
(filter #(gears (first %)))
(map #(update % 1 (partial map second)))
(map second)
(map #(second (update % 1 (partial map second))))
(map (partial map #(Integer/parseInt %)))
(map (partial reduce *))
(reduce +)))
(reduce +)
)