-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-21.clj
68 lines (58 loc) · 1.88 KB
/
day-21.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(ns day-21
(:require [hashp.core]))
(def parse-int #(Integer/parseInt %))
(defn parse-ingredients [s]
(->> (re-seq #"\w+" s)
(partition-by #{"contains"})
(remove #{'("contains")})
(map set)
(zipmap [:ingredients :allergens])))
(defn allergen->ingredients [{:keys [ingredients allergens]}]
(zipmap allergens (repeat ingredients)))
(defn intersect [xs]
(apply merge-with clojure.set/intersection (zipmap (mapcat keys xs) (mapcat vals xs)) xs))
(defn solve1 [ingredients]
(let [all (->> ingredients (map :ingredients))
union (apply clojure.set/union all)
flat (apply concat all)]
(->> ingredients
(map allergen->ingredients)
(intersect)
(vals)
(apply clojure.set/union)
(clojure.set/difference union)
(#(filter % flat))
(count))))
(defn puzzle1 [input]
(->> input
clojure.string/split-lines
(map parse-ingredients)
(solve1)))
(defn find-ingredient [acc remaining [x & xs]]
(let [seen (set (keys acc))
allergen (:allergen x)
[i & is] (clojure.set/difference (:ingredients x) seen)]
(cond
(and (nil? x) (empty? remaining)) acc
(nil? x) (recur acc '() remaining)
(empty? is) (recur (assoc acc i allergen) remaining xs)
:else (recur acc (conj remaining x) xs))))
(defn solve2 [ingredients]
(->> ingredients
(map allergen->ingredients)
(intersect)
(map #(zipmap [:allergen :ingredients] %))
(sort-by (comp count :ingredients))
(find-ingredient {} '())
(map #(zipmap [:ingredient :allergen] %))
(sort-by :allergen)
(map :ingredient)
(clojure.string/join ",")))
(defn puzzle2 [input]
(->> input
clojure.string/split-lines
(map parse-ingredients)
(solve2)))
(def input (->> (slurp "input21.txt")))
(time (puzzle1 input))
(time (puzzle2 input))