This commit is contained in:
Adam Jeniski 2025-10-19 06:29:14 -09:00
parent 63cd8c779c
commit c577615973

View File

@ -2,32 +2,48 @@
(:require [clojure.string :as str] (:require [clojure.string :as str]
input-manager)) input-manager))
(let [[w _ & p] (def word-sizes (memoize (comp distinct (partial map count))))
(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 (defn increment-counts-for-matching-words [words puzzle counts pos]
([puzzle] (let [weight (if (= pos 0)
(valid-combination-count puzzle 0 1 (mapv (constantly 0) puzzle))) 1
;; dp is vec of ints which contains the count of combinations for substring from puzzle[0] and ending at puzzle[n] (get counts (dec pos)))]
([puzzle pos weight dp] (->> (map (partial + pos) (word-sizes words))
(if (>= pos (count puzzle))
(last dp)
(let [dp' (->> (map (partial + pos) word-sizes)
(filter (partial >= (count puzzle))) ;; prevent NPE on substring overflow (filter (partial >= (count puzzle))) ;; prevent NPE on substring overflow
(map (partial subs puzzle pos)) (map (partial subs puzzle pos))
(filter words) (filter words)
(map (comp (partial + pos) dec count)) (map (comp (partial + pos) dec count))
(reduce (fn [acc idx] (update acc idx (partial + weight))) (reduce (fn [acc idx] (update acc idx (partial + weight)))
dp))] counts))))
(recur puzzle (inc pos) (get dp' pos) dp')))))
(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))
;; part 1 solution ;; part 1 solution
(->> (map valid-combination-count puzzles) (->> (map (partial valid-combination-count words) puzzles)
(filter (partial < 0)) (filter (partial < 0))
count) count)
;; part 2 solution ;; 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")
)