-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
880 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 (apply str 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 [[: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-file] | ||
(let [[wrkfls rtngs] (aoc/read-input-paragraphs input-file) | ||
workflows (into {} (map parse-workflow wrkfls)) | ||
ratings (map parse-rating rtngs) | ||
rating-2 (zipmap [:x :m :a :s] (repeat [1 4000]))] | ||
[(reduce + (map (partial accepted workflows) ratings)) | ||
(accepted-combinations workflows rating-2)])) | ||
|
||
|
||
(solve 19) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.