format, add comments

This commit is contained in:
Adam Jeniski 2023-12-03 13:13:45 -05:00
parent 9ddce5f7a8
commit d6a9747af2

View File

@ -3,9 +3,6 @@
day03 (:require day03 (:require
[core :refer [get-puzzle-input re-seq-pos]])) [core :refer [get-puzzle-input re-seq-pos]]))
(defn parse-nums [line]
(re-seq-pos #"\d+" line))
(def lines (get-puzzle-input 3)) (def lines (get-puzzle-input 3))
(def char-map (def char-map
(->> lines (->> lines
@ -16,13 +13,17 @@
(mapcat identity) (mapcat identity)
(into {}))) (into {})))
;; part 1 ;; produces a list of [row-idx col-idx "num"]
(defn parse-nums [lines]
(->> lines (->> lines
(map parse-nums) (map #(re-seq-pos #"\d+" %))
(map-indexed (fn [row matches] (map-indexed (fn [row matches]
(map #(vector row (:start %) (:group %)) (map #(vector row (:start %) (:group %))
matches))) matches)))
(mapcat identity) (mapcat identity)))
;; part 1
(->> (parse-nums lines)
(filter (fn touches-symbol? [[row-idx col-idx s]] (filter (fn touches-symbol? [[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)]
@ -34,12 +35,9 @@
(reduce +)) (reduce +))
;; part 2 ;; part 2
(let [data (->> lines ;; stars is a list of [[star-row-idx star-col-idx] "num"]
(map parse-nums) ;; stars is list of each * char found touching a number when iterating by nums
(map-indexed (fn [row matches] (let [stars (->> (parse-nums lines)
(map #(vector row (:start %) (:group %))
matches)))
(mapcat identity)
(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)]
@ -51,15 +49,18 @@
(filter seq) (filter seq)
(mapcat identity) (mapcat identity)
(map #(vector (ffirst %) (second %)))) (map #(vector (ffirst %) (second %))))
gear (->> data
;; 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
(map first) (map first)
(frequencies) (frequencies)
(filter #(= (second %) 2)) (filter #(= (second %) 2))
(map first) (map first)
(into #{}))] (into #{}))]
(->> data (->> stars
(group-by first) (group-by first)
(filter #(gear (first %))) (filter #(gears (first %)))
(map #(update % 1 (partial map second))) (map #(update % 1 (partial map second)))
(map second) (map second)
(map (partial map #(Integer/parseInt %))) (map (partial map #(Integer/parseInt %)))