Skip to content

Instantly share code, notes, and snippets.

@leetwinski
Last active December 14, 2020 17:03
Show Gist options
  • Save leetwinski/791aeb2ec73e2f41933bebb04429f5f3 to your computer and use it in GitHub Desktop.
Save leetwinski/791aeb2ec73e2f41933bebb04429f5f3 to your computer and use it in GitHub Desktop.
advent of code 2020 / 7
(defn parse-bag-rule [s]
(let [[_ bag contents] (re-matches #"^(.*?) bags contain (.*)$" s)
bags (re-seq #"(\d+) (\w+ \w+)" contents)]
[bag (into {} (map (fn [[_ n tag]]
[tag (clojure.edn/read-string n)])
bags))]))
(def bags (into {} (map parse-bag-rule (clojure.string/split-lines input-7))))
;; part 1
(->> bags
keys
(filter #(->> (tree-seq (constantly true) (comp keys bags) %)
rest
(some #{"shiny gold"})))
count)
;; part 2
(->> ["shiny gold" 1]
(tree-seq (constantly true)
(fn [[tag amount]]
(map #(update % 1 * amount) (bags tag))))
(map second)
(apply +)
dec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment