Skip to content

Commit

Permalink
day19.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Dec 19, 2023
1 parent 1094a7c commit 530f36c
Show file tree
Hide file tree
Showing 5 changed files with 880 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Day 00: Helper file | [aoc.clj](clojure/aoc.clj) |
[Day 16](http://adventofcode.com/2023/day/16) | [day16.clj](clojure/day16.clj) | Is this a cousin of Day 10?
[Day 17](http://adventofcode.com/2023/day/17) | [day17.clj](clojure/day17.clj) | Consider only L/R directions, and then go straight as far as you can.
[Day 18](http://adventofcode.com/2023/day/18) | [day18.clj](clojure/day18.clj) | TIL about Shoelace formula and Pick's theorem.
<!-- [Day 19](http://adventofcode.com/2023/day/19) | [day19.clj](clojure/day19.clj) | -->
[Day 19](http://adventofcode.com/2023/day/19) | [day19.clj](clojure/day19.clj) | Simpler ranges than Day 5.
<!-- [Day 20](http://adventofcode.com/2023/day/20) | [day20.clj](clojure/day20.clj) | -->
<!-- [Day 21](http://adventofcode.com/2023/day/21) | [day21.clj](clojure/day21.clj) | -->
<!-- [Day 22](http://adventofcode.com/2023/day/22) | [day22.clj](clojure/day22.clj) | -->
Expand Down
79 changes: 79 additions & 0 deletions clojure/day19.clj
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)
3 changes: 2 additions & 1 deletion clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
day01 day02 day03 day04 day05
day06 day07 day08 day09 day10
day11 day12 day13 day14 day15
day16 day17 day18 ; day19 day20
day16 day17 day18 day19 ;day20
; day21 day22 day23 day24 day25
[clojure.test :refer [deftest is run-tests successful?]]))

Expand Down Expand Up @@ -42,6 +42,7 @@
(check-day 16 [46 51] [6921 7594])
(check-day 17 [102 94] [758 892])
(check-day 18 [62 952408144115] [47045 147839570293376])
(check-day 19 [19114 167409079868000] [386787 131029523269531])

(let [summary (run-tests)]
(when-not (successful? summary)
Expand Down
Loading

0 comments on commit 530f36c

Please sign in to comment.