From c577615973c0405e842c68b9935c6dadfdcdef2b Mon Sep 17 00:00:00 2001 From: ajet Date: Sun, 19 Oct 2025 06:29:14 -0900 Subject: [PATCH] golf --- 2024/src/day19.clj | 56 +++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/2024/src/day19.clj b/2024/src/day19.clj index 7dbf40c..694eb4d 100644 --- a/2024/src/day19.clj +++ b/2024/src/day19.clj @@ -2,32 +2,48 @@ (:require [clojure.string :as str] input-manager)) +(def word-sizes (memoize (comp distinct (partial map count)))) + +(defn increment-counts-for-matching-words [words puzzle counts pos] + (let [weight (if (= pos 0) + 1 + (get counts (dec pos)))] + (->> (map (partial + pos) (word-sizes words)) + (filter (partial >= (count puzzle))) ;; prevent NPE on substring overflow + (map (partial subs puzzle pos)) + (filter words) + (map (comp (partial + pos) dec count)) + (reduce (fn [acc idx] (update acc idx (partial + weight))) + counts)))) + +(defn valid-combination-count [words puzzle] + (->> (range 0 (count puzzle)) + (reduce (partial increment-counts-for-matching-words words puzzle) + (mapv (constantly 0) puzzle)) + last)) + (let [[w _ & p] (input-manager/get-input 2024 19)] (def words (into #{} (str/split w #", "))) - (def puzzles p) - (def word-sizes (->> words (map count) distinct))) - -(defn valid-combination-count - ([puzzle] - (valid-combination-count puzzle 0 1 (mapv (constantly 0) puzzle))) - ;; dp is vec of ints which contains the count of combinations for substring from puzzle[0] and ending at puzzle[n] - ([puzzle pos weight dp] - (if (>= pos (count puzzle)) - (last dp) - (let [dp' (->> (map (partial + pos) word-sizes) - (filter (partial >= (count puzzle))) ;; prevent NPE on substring overflow - (map (partial subs puzzle pos)) - (filter words) - (map (comp (partial + pos) dec count)) - (reduce (fn [acc idx] (update acc idx (partial + weight))) - dp))] - (recur puzzle (inc pos) (get dp' pos) dp'))))) + (def puzzles p)) ;; part 1 solution -(->> (map valid-combination-count puzzles) +(->> (map (partial valid-combination-count words) puzzles) (filter (partial < 0)) count) ;; part 2 solution -(->> (map valid-combination-count puzzles) (apply +)) +(apply + (map (partial valid-combination-count words) puzzles)) + +(comment + (increment-counts-for-matching-words #{"a"} "atest" [0 0 0 0 0 0] 0) ; [1 0 0 0 0 0] + (increment-counts-for-matching-words #{"a"} "btest" [0 0 0 0 0 0] 0) ; [0 0 0 0 0 0] + (increment-counts-for-matching-words #{"te"} "atest" [1 0 0 0 0 0] 1) ; [1 0 1 0 0 0] + + (increment-counts-for-matching-words #{"wo" "rd"} "word" [0 1 0 0] 2) ; [0 1 0 1] + (valid-combination-count #{"wo" "rd"} "word") ; 1 + (valid-combination-count #{"word"} "word") ; 1 + + (increment-counts-for-matching-words #{"wo" "or" "w" "rd" "d"} "word" [0 0 0 0] 0) ; [1 1 0 0] + (valid-combination-count #{"wo" "or" "w" "rd" "d"} "word") ; 2, because ("wo" "r" "d") and ("w") + )