mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-11-27 14:02:44 -10:00
Compare commits
No commits in common. "43c69f4e0b557183642c2678fcb5a9abef6e07ec" and "bf47ff7c34da5aaa819c4a1dc8e0c71d0e868294" have entirely different histories.
43c69f4e0b
...
bf47ff7c34
@ -1,43 +1,85 @@
|
|||||||
(ns day19
|
(ns day19
|
||||||
(:require
|
(:require
|
||||||
[clojure.string :as str]
|
[clojure.string :as str] ; [core :as c]
|
||||||
input-manager))
|
input-manager
|
||||||
|
[orchard.pp :as pp]))
|
||||||
|
|
||||||
|
(def sample-input "r, wr, b, g, bwu, rb, gb, br
|
||||||
|
|
||||||
|
brwrr
|
||||||
|
bggr
|
||||||
|
gbbr
|
||||||
|
rrbgbr
|
||||||
|
ubwu
|
||||||
|
bwurrg
|
||||||
|
brgr
|
||||||
|
bbrgwb")
|
||||||
|
|
||||||
(let [[w _ & p]
|
(let [[w _ & p]
|
||||||
|
#_(str/split-lines sample-input)
|
||||||
(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
|
(def word-sizes (->> words
|
||||||
(map count)
|
(map count)
|
||||||
distinct)))
|
distinct
|
||||||
|
sort
|
||||||
|
reverse)))
|
||||||
|
|
||||||
(defn valid-combination-count
|
(defn p [x]
|
||||||
|
(println x)
|
||||||
|
x)
|
||||||
|
|
||||||
|
(defn next-search-index [puzzle searched valid-subsets]
|
||||||
|
(if (every? (complement true?) searched)
|
||||||
|
0
|
||||||
|
(some->> valid-subsets
|
||||||
|
(map-indexed vector)
|
||||||
|
(filter second)
|
||||||
|
(filter (fn [[idx :as el]]
|
||||||
|
(not (searched (inc idx)))))
|
||||||
|
last
|
||||||
|
first
|
||||||
|
inc)))
|
||||||
|
|
||||||
|
(defn find-valid-subsets [puzzle search-idx valid-subsets]
|
||||||
|
(let [max-idx (count puzzle)]
|
||||||
|
(->> word-sizes
|
||||||
|
(map (partial + search-idx)) ; get end pos
|
||||||
|
(filter #(<= % max-idx))
|
||||||
|
(map (partial subs puzzle search-idx)) ;get substrings
|
||||||
|
(filter words)
|
||||||
|
(map (comp (partial + search-idx) dec count))
|
||||||
|
(reduce (fn [acc idx]
|
||||||
|
(assoc acc idx true))
|
||||||
|
valid-subsets))))
|
||||||
|
|
||||||
|
(defn is-valid
|
||||||
([puzzle]
|
([puzzle]
|
||||||
(valid-combination-count puzzle
|
(is-valid puzzle
|
||||||
0
|
(mapv (constantly false) puzzle)
|
||||||
1
|
(mapv (constantly false) puzzle)
|
||||||
(mapv (constantly 0) puzzle)))
|
0))
|
||||||
;; dp is vec of ints which contains the count of combinations for substring from puzzle[0] and ending at puzzle[n]
|
([puzzle searched valid-subsets n]
|
||||||
([puzzle pos weight dp]
|
(let [search-idx (next-search-index puzzle searched valid-subsets)]
|
||||||
(if (>= pos (count puzzle))
|
(cond
|
||||||
(last dp)
|
(or (not search-idx)
|
||||||
(let [dp' (->> (map (partial + pos) word-sizes)
|
(= search-idx -1))
|
||||||
(filter (partial >= (count puzzle)))
|
false
|
||||||
(map (partial subs puzzle pos))
|
|
||||||
(filter words)
|
(>= search-idx (count puzzle))
|
||||||
(map (comp dec count))
|
true
|
||||||
(map (partial + pos))
|
|
||||||
(reduce (fn [acc idx] (update acc idx (partial + weight)))
|
:else
|
||||||
dp))]
|
(let [next-subsets (find-valid-subsets puzzle search-idx valid-subsets)]
|
||||||
(recur puzzle (inc pos) (get dp' pos) dp')))))
|
(if (last next-subsets)
|
||||||
|
true
|
||||||
|
(recur puzzle
|
||||||
|
(assoc searched search-idx true)
|
||||||
|
next-subsets
|
||||||
|
(inc n))))))))
|
||||||
|
|
||||||
;; part 1 solution
|
|
||||||
(->> puzzles
|
(->> puzzles
|
||||||
(map valid-combination-count)
|
(map is-valid)
|
||||||
(filter (partial < 0))
|
(filter identity)
|
||||||
count)
|
count)
|
||||||
|
|
||||||
;; part 2 solution
|
|
||||||
(->> puzzles
|
|
||||||
(map valid-combination-count)
|
|
||||||
(apply +))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user