day 4, part 2
This commit is contained in:
parent
92beca4d61
commit
3bc28a49d8
@ -8,23 +8,48 @@
|
|||||||
|
|
||||||
(defn parse-line [l]
|
(defn parse-line [l]
|
||||||
(let [m (rest (re-find #"Card +(\d+): +([\d ]+) \| +([\d ]+)" l))]
|
(let [m (rest (re-find #"Card +(\d+): +([\d ]+) \| +([\d ]+)" l))]
|
||||||
[(read-string (first m)) (mapv parse-numbers (rest m))]))
|
[(read-string (first m))
|
||||||
|
(assoc
|
||||||
|
(zipmap [:winning :had] (mapv parse-numbers (rest m)))
|
||||||
|
:copies 0)]))
|
||||||
|
|
||||||
(def input
|
(def input
|
||||||
(->> "inputs/day04"
|
(->> "inputs/day04"
|
||||||
slurp string/split-lines
|
slurp string/split-lines
|
||||||
(mapcat parse-line)
|
(mapcat parse-line)
|
||||||
(apply hash-map)))
|
(apply sorted-map)))
|
||||||
|
|
||||||
(defn count-winning [[_ [winning had]]]
|
(defn count-winning [{:keys [winning had]}]
|
||||||
(->> (map #(some #{%} winning) had)
|
(->> (map #(some #{%} winning) had)
|
||||||
(remove nil?)
|
(remove nil?)
|
||||||
count))
|
count))
|
||||||
|
|
||||||
(def part1-answer
|
(def part1-answer
|
||||||
(->> (map count-winning input)
|
(->> (vals input)
|
||||||
|
(map count-winning)
|
||||||
(map #(case %
|
(map #(case %
|
||||||
0 0
|
0 0
|
||||||
1 1
|
1 1
|
||||||
(pow 2 (dec %))))
|
(pow 2 (dec %))))
|
||||||
(reduce +)))
|
(reduce +)))
|
||||||
|
|
||||||
|
(def part2-answer
|
||||||
|
(->>
|
||||||
|
(loop [cards input
|
||||||
|
todo input]
|
||||||
|
(if (empty? todo)
|
||||||
|
cards
|
||||||
|
(let [current (first todo)
|
||||||
|
current-id (key current)
|
||||||
|
current-copies (inc (:copies (val current)))
|
||||||
|
matching (count-winning (val current))
|
||||||
|
to-win (range (inc current-id)
|
||||||
|
(inc (+ current-id matching)))
|
||||||
|
cards' (reduce
|
||||||
|
(fn [cards i]
|
||||||
|
(update-in cards [i :copies] (partial + current-copies)))
|
||||||
|
cards
|
||||||
|
to-win)]
|
||||||
|
(recur cards' (drop current-id cards')))))
|
||||||
|
(map #(inc (get (val %) :copies)))
|
||||||
|
(reduce +)))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user