File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ (ns scratchcards
2+ (:require [clojure.string :as str]
3+ [clojure.set :as set]
4+ [clojure.math :as math]))
5+
6+ (defn count-winning [line]
7+ (let [[_card-number & numbers]
8+ (re-seq #"(?:\d +|\| )" line)
9+
10+ [guessed-numbers _ drawn-numbers]
11+ (partition-by (partial = " |" ) numbers)]
12+ (count (set/intersection (set guessed-numbers)
13+ (set drawn-numbers)))))
14+
15+ (defn score [winning-count]
16+ (if (zero? winning-count)
17+ 0
18+ (int (math/pow 2 (dec winning-count)))))
19+
20+ ; ; part 1
21+
22+ (->> (slurp " 2023/04.in" )
23+ (str/split-lines )
24+ (map (comp score count-winning))
25+ (reduce +))
26+
27+ ; ; part 2
28+
29+ (defn beget-cards [idx winning count cards]
30+ (reduce (fn [cards idx] (update-in cards [idx :count ] + count))
31+ cards
32+ (range (inc idx) (inc (+ idx winning)))))
33+
34+ (let [i (volatile! -1 )
35+ cards (->> (slurp " 2023/04.in" )
36+ (str/split-lines )
37+ (mapv (fn [line] {:winning (count-winning line), :count 1 })))]
38+ (->> (reduce (fn [cards _]
39+ (vswap! i inc)
40+ (let [{:keys [winning count]} (get cards @i)]
41+ (beget-cards @i winning count cards)))
42+ cards
43+ (range (count cards)))
44+ (map :count )
45+ (reduce +)))
You can’t perform that action at this time.
0 commit comments