-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19.clj
79 lines (64 loc) · 2.26 KB
/
day19.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(ns day19
(:require aoc
[clojure.string :as str]))
(defn parse-rule [rule]
(let [[cnd dest] (str/split rule #":")]
(if (nil? dest)
[(keyword cnd)]
(let [[c op & v] cnd
k (keyword (str c))
f ({\> > \< <} op)
v (parse-long (str/join v))]
[(keyword dest) f k v]))))
(defn parse-workflow [workflow]
(let [[_ k rules] (re-find #"(\w+)\{(.+)\}" workflow)
k (keyword k)
rules (map parse-rule (str/split rules #","))]
[k rules]))
(defn parse-rating [rating]
(let [[x m a s] (aoc/integers rating)]
{:x x , :m m , :a a , :s s}))
(defn accepted [workflows rating]
(loop [wf :in]
(case wf
:R 0
:A (reduce + (vals rating))
(recur (loop [[[dest f k v] & tl] (workflows wf)]
(cond
(nil? f) dest
(f (rating k) v) dest
:else (recur tl)))))))
(defn split-out [workflow rating]
(loop [[[dest f k v] & tl] workflow
out []
rating rating]
(cond
(nil? dest) out
(nil? f) (conj out [dest rating])
(= f <) (let [[a b] (rating k)]
(recur tl
(conj out [dest (assoc rating k [a (dec v)])])
(assoc rating k [v b])))
(= f >) (let [[a b] (rating k)]
(recur tl
(conj out [dest (assoc rating k [(inc v) b])])
(assoc rating k [a v]))))))
(defn accepted-combinations [workflows rating]
(loop [stack (list [:in rating])
score 0]
(if-let [[wf rating] (peek stack)]
(let [stack' (pop stack)]
(case wf
:R (recur stack' score)
:A (recur stack' (+ score (reduce * (map (fn [[a b]] (- (inc b) a))
(vals rating)))))
(recur (reduce conj stack' (split-out (workflows wf) rating)) score)))
score)))
(defn solve [input]
(let [[wrkfls rtngs] (aoc/parse-input-paragraphs input)
workflows (into {} (map parse-workflow wrkfls))
ratings (map parse-rating rtngs)
rating-2 (zipmap [:x :m :a :s] (repeat [1 4000]))]
[(aoc/sum-map #(accepted workflows %) ratings)
(accepted-combinations workflows rating-2)]))
(solve (aoc/read-file 19))