Last active
December 14, 2020 17:03
-
-
Save leetwinski/791aeb2ec73e2f41933bebb04429f5f3 to your computer and use it in GitHub Desktop.
advent of code 2020 / 7
This file contains hidden or 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
| (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