do day 7
This commit is contained in:
parent
ac6c0097e6
commit
4e548d4d40
55
2025/src/day07.clj
Normal file
55
2025/src/day07.clj
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
(ns day07
|
||||||
|
(:require input-manager
|
||||||
|
core))
|
||||||
|
|
||||||
|
(def input (input-manager/get-input 2025 7))
|
||||||
|
|
||||||
|
(def start ((core/map-to-coords input) \S))
|
||||||
|
(def grid (core/map-by-coords input))
|
||||||
|
(def NUM_ROWS (count input))
|
||||||
|
|
||||||
|
(defn left [loc] (update loc 1 dec))
|
||||||
|
(defn right [loc] (update loc 1 inc))
|
||||||
|
(defn down [loc] (update loc 0 inc))
|
||||||
|
(defn splitter? [loc] (= (grid loc) \^))
|
||||||
|
|
||||||
|
;; part 1
|
||||||
|
(->> (range NUM_ROWS)
|
||||||
|
(reduce (fn [{:keys [acc search-locs]} _]
|
||||||
|
(if (empty? search-locs)
|
||||||
|
(reduced acc)
|
||||||
|
(let [next-locs (map down search-locs)
|
||||||
|
splitters (filter splitter? next-locs)
|
||||||
|
next-locs (-> #{}
|
||||||
|
(into (filter (complement splitter?) next-locs))
|
||||||
|
(into (mapcat (juxt left right) splitters)))]
|
||||||
|
{:acc (into acc splitters)
|
||||||
|
:search-locs next-locs})))
|
||||||
|
{:acc #{}, :search-locs #{start}})
|
||||||
|
:acc
|
||||||
|
count)
|
||||||
|
|
||||||
|
(defn left-entry [[loc cnt]] [(left loc) cnt])
|
||||||
|
(defn right-entry [[loc cnt]] [(right loc) cnt])
|
||||||
|
(defn splitter-entry? [[loc _]] (= (grid loc) \^))
|
||||||
|
(defn join-flows [flows]
|
||||||
|
(reduce (fn [acc [loc cnt]]
|
||||||
|
(if (acc loc)
|
||||||
|
(update acc loc + cnt)
|
||||||
|
(assoc acc loc cnt)))
|
||||||
|
{}
|
||||||
|
flows))
|
||||||
|
|
||||||
|
;; part 2
|
||||||
|
(->> (range NUM_ROWS)
|
||||||
|
(reduce (fn [{:keys [acc search-locs]} _]
|
||||||
|
(if (empty? search-locs)
|
||||||
|
(reduced acc)
|
||||||
|
(let [next-locs (update-keys search-locs down)
|
||||||
|
splitters (filter splitter-entry? next-locs)
|
||||||
|
next-locs (join-flows (concat (filter (complement splitter-entry?) next-locs)
|
||||||
|
(mapcat (juxt left-entry right-entry) splitters)))]
|
||||||
|
{:acc (apply + acc (map (fn [[_ cnt]] cnt) splitters))
|
||||||
|
:search-locs next-locs})))
|
||||||
|
{:acc 1, :search-locs {start 1}})
|
||||||
|
:acc)
|
||||||
Loading…
x
Reference in New Issue
Block a user