mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-11-27 11:32:45 -10:00
golf
This commit is contained in:
parent
63cd8c779c
commit
c577615973
@ -2,32 +2,48 @@
|
|||||||
(:require [clojure.string :as str]
|
(:require [clojure.string :as str]
|
||||||
input-manager))
|
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]
|
(let [[w _ & p]
|
||||||
(input-manager/get-input 2024 19)]
|
(input-manager/get-input 2024 19)]
|
||||||
(def words (into #{} (str/split w #", ")))
|
(def words (into #{} (str/split w #", ")))
|
||||||
(def puzzles p)
|
(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')))))
|
|
||||||
|
|
||||||
;; 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")
|
||||||
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user