Skip to content

Instantly share code, notes, and snippets.


devn/day7.clj Secret

Created Dec 8, 2020
What would you like to do?
clara-based solve of Part I of Day 7 Advent of Code
(defrecord ContainerBag [type contained-bags])
(defrecord IntermediateBag [parent-type type])
(defrecord GoldBag [parent-type])
(defrule produce-intermediate-bags
(= ?type type)
(= ?contained-bags contained-bags)]
(when (seq ?contained-bags)
(mapv (fn [[kind num]]
(if (= kind "shiny gold")
(map->GoldBag {:parent-type ?type})
(map->IntermediateBag {:parent-type ?type
:type kind})))
(defrule indirect-gold-bags
(= ?type type)]
(= ?type parent-type)
(= ?intermediate-type type)]
(= ?intermediate-type parent-type)]
(insert! (map->GoldBag {:parent-type ?type})))
(defquery gold-bags []
[?gold-bags <- (acc/distinct :parent-type) :from [GoldBag]])
(defn run-rules []
(-> (mk-session :cache false)
(insert-all (doall (for [[k vs] (parse input)]
(map->ContainerBag {:type k
:contained-bags vs}))))
;; Part I
(-> (run-rules)
(query gold-bags)
;; => 179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment