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.

Revisions

  1. leetwinski revised this gist Dec 14, 2020. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions main.clj
    Original file line number Diff line number Diff line change
    @@ -10,11 +10,9 @@
    ;; part 1
    (->> bags
    keys
    (filter (fn [label]
    (->> label
    (tree-seq (constantly true) (comp keys bags))
    rest
    (some #{"shiny gold"}))))
    (filter #(->> (tree-seq (constantly true) (comp keys bags) %)
    rest
    (some #{"shiny gold"})))
    count)

    ;; part 2
  2. leetwinski created this gist Dec 14, 2020.
    27 changes: 27 additions & 0 deletions main.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    (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 (fn [label]
    (->> label
    (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)