mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
refactor, add more comments
This commit is contained in:
parent
d6a9747af2
commit
34a17e5edb
@ -4,6 +4,8 @@
|
|||||||
[core :refer [get-puzzle-input re-seq-pos]]))
|
[core :refer [get-puzzle-input re-seq-pos]]))
|
||||||
|
|
||||||
(def lines (get-puzzle-input 3))
|
(def lines (get-puzzle-input 3))
|
||||||
|
|
||||||
|
;; a hash-map of [row-idx col-idx] to char
|
||||||
(def char-map
|
(def char-map
|
||||||
(->> lines
|
(->> lines
|
||||||
(map-indexed (fn [row-idx line]
|
(map-indexed (fn [row-idx line]
|
||||||
@ -34,36 +36,37 @@
|
|||||||
(map #(Integer/parseInt (nth % 2)))
|
(map #(Integer/parseInt (nth % 2)))
|
||||||
(reduce +))
|
(reduce +))
|
||||||
|
|
||||||
;; part 2
|
|
||||||
;; 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
|
||||||
(let [stars (->> (parse-nums lines)
|
(def stars (->> (parse-nums lines)
|
||||||
(map (fn touching-stars [[row-idx col-idx s]]
|
(map (fn touching-stars [[row-idx col-idx s]]
|
||||||
(let [length (count s)]
|
(let [length (count s)]
|
||||||
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
|
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
|
||||||
c (range (dec col-idx) (+ col-idx length 1))]
|
c (range (dec col-idx) (+ col-idx length 1))]
|
||||||
[[r c] (char-map [r c])])
|
[[r c] (char-map [r c])])
|
||||||
(filter #(not (nil? (second %))))
|
(filter #(not (nil? (second %))))
|
||||||
(filter #(= \* (second %)))
|
(filter #(= \* (second %)))
|
||||||
(map #(vector % s))))))
|
(map #(vector % s))))))
|
||||||
(filter seq)
|
(filter seq)
|
||||||
(mapcat identity)
|
(mapcat identity)
|
||||||
(map #(vector (ffirst %) (second %))))
|
(map #(vector (ffirst %) (second %)))))
|
||||||
|
|
||||||
;; 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
|
||||||
gears (->> stars
|
(def gears (->> stars
|
||||||
(map first)
|
(map first)
|
||||||
(frequencies)
|
(frequencies)
|
||||||
(filter #(= (second %) 2))
|
(filter #(= (second %) 2))
|
||||||
(map first)
|
(map first)
|
||||||
(into #{}))]
|
(into #{})))
|
||||||
(->> stars
|
|
||||||
(group-by first)
|
;; part 2
|
||||||
(filter #(gears (first %)))
|
(->> stars
|
||||||
(map #(update % 1 (partial map second)))
|
(group-by first)
|
||||||
(map second)
|
(filter #(gears (first %)))
|
||||||
(map (partial map #(Integer/parseInt %)))
|
(map #(second (update % 1 (partial map second))))
|
||||||
(map (partial reduce *))
|
(map (partial map #(Integer/parseInt %)))
|
||||||
(reduce +)))
|
(map (partial reduce *))
|
||||||
|
(reduce +)
|
||||||
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user