mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 07:03:19 -09:00
format, add comments
This commit is contained in:
parent
9ddce5f7a8
commit
d6a9747af2
@ -3,9 +3,6 @@
|
||||
day03 (:require
|
||||
[core :refer [get-puzzle-input re-seq-pos]]))
|
||||
|
||||
(defn parse-nums [line]
|
||||
(re-seq-pos #"\d+" line))
|
||||
|
||||
(def lines (get-puzzle-input 3))
|
||||
(def char-map
|
||||
(->> lines
|
||||
@ -16,13 +13,17 @@
|
||||
(mapcat identity)
|
||||
(into {})))
|
||||
|
||||
;; produces a list of [row-idx col-idx "num"]
|
||||
(defn parse-nums [lines]
|
||||
(->> lines
|
||||
(map #(re-seq-pos #"\d+" %))
|
||||
(map-indexed (fn [row matches]
|
||||
(map #(vector row (:start %) (:group %))
|
||||
matches)))
|
||||
(mapcat identity)))
|
||||
|
||||
;; part 1
|
||||
(->> lines
|
||||
(map parse-nums)
|
||||
(map-indexed (fn [row matches]
|
||||
(map #(vector row (:start %) (:group %))
|
||||
matches)))
|
||||
(mapcat identity)
|
||||
(->> (parse-nums lines)
|
||||
(filter (fn touches-symbol? [[row-idx col-idx s]]
|
||||
(let [length (count s)]
|
||||
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
|
||||
@ -34,32 +35,32 @@
|
||||
(reduce +))
|
||||
|
||||
;; part 2
|
||||
(let [data (->> lines
|
||||
(map parse-nums)
|
||||
(map-indexed (fn [row matches]
|
||||
(map #(vector row (:start %) (:group %))
|
||||
matches)))
|
||||
(mapcat identity)
|
||||
(map (fn touching-stars [[row-idx col-idx s]]
|
||||
(let [length (count s)]
|
||||
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
|
||||
c (range (dec col-idx) (+ col-idx length 1))]
|
||||
[[r c] (char-map [r c])])
|
||||
(filter #(not (nil? (second %))))
|
||||
(filter #(= \* (second %)))
|
||||
(map #(vector % s))))))
|
||||
(filter seq)
|
||||
(mapcat identity)
|
||||
(map #(vector (ffirst %) (second %))))
|
||||
gear (->> data
|
||||
(map first)
|
||||
(frequencies)
|
||||
(filter #(= (second %) 2))
|
||||
(map first)
|
||||
(into #{}))]
|
||||
(->> data
|
||||
;; 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)
|
||||
(map (fn touching-stars [[row-idx col-idx s]]
|
||||
(let [length (count s)]
|
||||
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
|
||||
c (range (dec col-idx) (+ col-idx length 1))]
|
||||
[[r c] (char-map [r c])])
|
||||
(filter #(not (nil? (second %))))
|
||||
(filter #(= \* (second %)))
|
||||
(map #(vector % s))))))
|
||||
(filter seq)
|
||||
(mapcat identity)
|
||||
(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
|
||||
(map first)
|
||||
(frequencies)
|
||||
(filter #(= (second %) 2))
|
||||
(map first)
|
||||
(into #{}))]
|
||||
(->> stars
|
||||
(group-by first)
|
||||
(filter #(gear (first %)))
|
||||
(filter #(gears (first %)))
|
||||
(map #(update % 1 (partial map second)))
|
||||
(map second)
|
||||
(map (partial map #(Integer/parseInt %)))
|
||||
|
Loading…
x
Reference in New Issue
Block a user