this repo has no description

(D4P2): Somewhat optimized rewrite (~6.5s -> ~255ms)

+59 -9
+59 -9
src/aoc2025/day4.clj
··· 5 5 (defn- parse-file [fname] 6 6 (->> fname util/read-file util/gridify)) 7 7 8 + (def paper? (partial = \@)) 9 + 8 10 (defn- -get-removable [grid] 9 11 (let [height (-> grid count) 10 12 width (-> grid first count) ··· 25 27 ; Laziest optimization in existence but it makes part2 run in 6.5s instead of like 19s so yay 26 28 (def get-removable (memoize -get-removable)) 27 29 28 - (defn- part1 [grid] 29 - (-> grid get-removable count)) 30 - 31 - (defn- remove-paper [grid] (reduce #(assoc-in %1 %2 \.) grid (get-removable grid))) 30 + (defn- valid-adj [[y x :as pos] width height] 31 + (letfn [(valid-coord? [[cy cx]] 32 + (and (util/between? cy -1 height >) 33 + (util/between? cx -1 width >)))] 34 + (->> pos util/adj+ 35 + (filter valid-coord?)))) 32 36 33 37 (defn- part2 [grid] 34 - (->> grid 35 - (iterate remove-paper) 36 - (take-while #(> (-> % get-removable count) 0)) 37 - (map (comp count get-removable)) 38 - (reduce +))) 38 + (let [grid-height (-> grid count) 39 + grid-width (-> grid first count) 40 + 41 + #_(First thing is to get a list of only the spots with paper on them) 42 + all-paper-spots 43 + (set 44 + (for [idy (range grid-height) 45 + idx (range grid-width) 46 + :let [pos [idy idx]] 47 + :when (paper? (get-in grid pos))] 48 + pos)) 49 + 50 + #_(For each paper spot, find all adjacent spots. Make a map of format {position [adjacent spots with paper]}) 51 + paper-spots-to-adjacent-paper-spots 52 + (into {} 53 + (for [pos all-paper-spots 54 + :let [adjacents (->> (valid-adj pos grid-width grid-height) 55 + (filter all-paper-spots))]] 56 + [pos adjacents])) 57 + 58 + 59 + removable? (fn [[_ v]] (-> v count (< 4))) 60 + any-removable? (fn [m] (some (fn [[_ v]] (removable? [nil v])) m)) 61 + 62 + #_(Deletes a paper spot and any references to it by its neighbors) 63 + remove-a-paper 64 + (fn [paper-spots [spot neighbors]] 65 + (-> (reduce #(update %1 %2 (partial remove #{spot})) paper-spots neighbors) 66 + (dissoc spot))) 67 + 68 + #_(Takes the current paper spots and returns a list of paper spots with all paper currently eligible to be remove removed) 69 + paper-iterator 70 + (fn [paper-spots] 71 + (let [to-remove (filter removable? paper-spots)] 72 + (reduce remove-a-paper paper-spots to-remove))) 73 + 74 + starting-paper-count (count all-paper-spots) 75 + 76 + final-state 77 + (->> paper-spots-to-adjacent-paper-spots 78 + (iterate paper-iterator) 79 + (split-with any-removable?) 80 + second 81 + first) 82 + 83 + final-paper-count (-> final-state keys count)] 84 + (- starting-paper-count final-paper-count))) 85 + 86 + (defn- part1 [grid] 87 + (-> grid get-removable count)) 39 88 40 89 (defn solve 41 90 ([] (solve "04.txt")) ··· 43 92 (->> (parse-file fname) ((juxt part1 part2))))) 44 93 45 94 (solve "04-test.txt") ; [13 43] 95 +