mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
format, add comments
This commit is contained in:
parent
9ddce5f7a8
commit
d6a9747af2
@ -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 {})))
|
||||||
|
|
||||||
|
;; 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
|
;; part 1
|
||||||
(->> lines
|
(->> (parse-nums lines)
|
||||||
(map parse-nums)
|
|
||||||
(map-indexed (fn [row matches]
|
|
||||||
(map #(vector row (:start %) (:group %))
|
|
||||||
matches)))
|
|
||||||
(mapcat identity)
|
|
||||||
(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,32 +35,32 @@
|
|||||||
(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 %))
|
(map (fn touching-stars [[row-idx col-idx s]]
|
||||||
matches)))
|
(let [length (count s)]
|
||||||
(mapcat identity)
|
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
|
||||||
(map (fn touching-stars [[row-idx col-idx s]]
|
c (range (dec col-idx) (+ col-idx length 1))]
|
||||||
(let [length (count s)]
|
[[r c] (char-map [r c])])
|
||||||
(->> (for [r [(dec row-idx) row-idx (inc row-idx)]
|
(filter #(not (nil? (second %))))
|
||||||
c (range (dec col-idx) (+ col-idx length 1))]
|
(filter #(= \* (second %)))
|
||||||
[[r c] (char-map [r c])])
|
(map #(vector % s))))))
|
||||||
(filter #(not (nil? (second %))))
|
(filter seq)
|
||||||
(filter #(= \* (second %)))
|
(mapcat identity)
|
||||||
(map #(vector % s))))))
|
(map #(vector (ffirst %) (second %))))
|
||||||
(filter seq)
|
|
||||||
(mapcat identity)
|
;; gears is a set of [star-row-idx star-col-idx]
|
||||||
(map #(vector (ffirst %) (second %))))
|
;; gears is a list of each * char that is touching exactly 2 nums
|
||||||
gear (->> data
|
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 %)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user