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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
import gleam/io
import gleam/int
import gleam/list
import gleam/pair
import gleam/string as str
import gleam/set.{type Set}
import gleam/dict.{type Dict}
import ext/setx
import ext/boolx
import ext/resultx as resx
import util/input_util
import util/parser as p
type Ingredient =
String
type Allergen =
String
type Food {
Food(ingredients: Set(Ingredient), some_allergens: Set(Allergen))
}
type Candidates =
Dict(Allergen, Set(Ingredient))
fn parse_food(text: String) -> Food {
let food_parser =
p.alpha1()
|> p.sep1(by: p.ws_gc())
|> p.map(with: set.from_list)
|> p.skip(p.literal(" (contains "))
|> p.then(
p.alpha1()
|> p.sep1(by: p.literal(", "))
|> p.map(with: set.from_list),
)
|> p.skip(p.literal(")"))
|> p.map2(with: Food)
text
|> p.parse_entire(with: food_parser)
|> resx.assert_unwrap
}
fn allergen_candidates(foods: List(Food)) -> Candidates {
foods
|> list.map(fn(f) { f.some_allergens })
|> setx.arbitrary_union
|> set.to_list
|> list.map(with: fn(allergen) {
#(
allergen,
foods
|> list.filter(keeping: fn(f) {
set.contains(in: f.some_allergens, this: allergen)
})
|> list.map(with: fn(f) { f.ingredients })
|> setx.arbitrary_intersection,
)
})
|> dict.from_list
}
fn stabilize(candidates: Candidates) -> Dict(Allergen, Ingredient) {
let is_stable = fn(s) { set.size(s) == 1 }
use <- boolx.guard_lazy(
when: candidates
|> dict.values
|> list.all(satisfying: is_stable),
return: fn() {
candidates
|> dict.map_values(with: fn(_, ingredients) {
let assert [ingredient] = set.to_list(ingredients)
ingredient
})
},
)
let stable_set =
candidates
|> dict.to_list
|> list.filter_map(with: fn(entry) {
let #(_, ingredients) = entry
case set.to_list(ingredients) {
[stable] -> Ok(stable)
_ -> Error(Nil)
}
})
|> set.from_list
candidates
|> dict.map_values(with: fn(_, ingredients) {
case is_stable(ingredients) {
True -> ingredients
False -> setx.subtract(from: ingredients, given: stable_set)
}
})
|> stabilize
}
fn part1(lines: List(String)) -> Int {
let foods = list.map(lines, with: parse_food)
let all_ingredients =
foods
|> list.map(fn(f) { f.ingredients })
|> setx.arbitrary_union
let candidates = allergen_candidates(foods)
let suspicious_ingredients =
candidates
|> dict.values
|> setx.arbitrary_union
let safe_ingredients =
setx.subtract(from: all_ingredients, given: suspicious_ingredients)
foods
|> list.map(with: fn(f) {
setx.count(f.ingredients, satisfying: set.contains(safe_ingredients, _))
})
|> int.sum
}
fn part2(lines: List(String)) -> String {
lines
|> list.map(with: parse_food)
|> allergen_candidates
|> stabilize
|> dict.to_list
|> list.sort(by: fn(a, b) { str.compare(pair.first(a), pair.first(b)) })
|> list.map(with: pair.second)
|> str.join(",")
}
pub fn main() -> Nil {
let testing = input_util.read_lines("test21")
let assert 5 = part1(testing)
let assert "mxmxvkd,sqjhc,fvjkl" = part2(testing)
let input = input_util.read_lines("day21")
io.debug(part1(input))
io.println(part2(input))
Nil
}
|