-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.rs
276 lines (251 loc) · 8.71 KB
/
day19.rs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use aoc_runner_derive::aoc;
fn parse(input: &str) -> utils::SortingSystem {
let (workflows, part_ratings) = input.split_once("\n\n").unwrap();
let workflows = workflows
.lines()
.map(|line| {
let (workflow_name, rules) = line.trim_end_matches('}').split_once('{').unwrap();
let rules = rules
.split(',')
.map(|rule| {
if let Some((condition, next_workflow)) = rule.split_once(':') {
let next_workflow = next_workflow.to_owned();
let category = condition.chars().next().unwrap().try_into().unwrap();
let value = condition[2..].parse().unwrap();
if condition.chars().nth(1).unwrap() == '<' {
utils::Condition::Less {
next_workflow,
category,
value,
}
} else {
utils::Condition::Greater {
next_workflow,
category,
value,
}
}
} else {
let next_workflow = rule.to_owned();
utils::Condition::None { next_workflow }
}
})
.collect();
(workflow_name.to_owned(), rules)
})
.collect();
let part_ratings = part_ratings
.lines()
.map(|line| {
line.trim_start_matches('{')
.trim_end_matches('}')
.split(',')
.map(|l| {
let (category, value) = l.split_once('=').unwrap();
let category = category.chars().next().unwrap().try_into().unwrap();
let value = value.parse().unwrap();
(category, value)
})
.collect()
})
.collect();
utils::SortingSystem {
workflows,
part_ratings,
}
}
#[aoc(day19, part1)]
#[must_use]
pub fn part1(input: &str) -> u64 {
let input = parse(input);
utils::count_accepted(&input, "in")
}
#[aoc(day19, part2)]
#[must_use]
pub fn part2(input: &str) -> u64 {
let input = parse(input);
use strum::IntoEnumIterator;
utils::count_all_accepted_combinations_recursively(
&input.workflows,
utils::Category::iter().map(|ch| (ch, (1, 4000))).collect(),
"in",
)
}
mod utils {
use strum::EnumIter;
pub struct SortingSystem {
pub workflows: rustc_hash::FxHashMap<String, smallvec::SmallVec<[Condition; 4]>>,
pub part_ratings: Vec<rustc_hash::FxHashMap<Category, u64>>,
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Condition {
Less {
next_workflow: String,
category: Category,
value: u64,
},
Greater {
next_workflow: String,
category: Category,
value: u64,
},
None {
next_workflow: String,
},
}
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
pub enum Category {
X = b'x',
M = b'm',
A = b'a',
S = b's',
}
impl TryFrom<char> for Category {
type Error = &'static str;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'x' => Ok(Category::X),
'm' => Ok(Category::M),
'a' => Ok(Category::A),
's' => Ok(Category::S),
_ => Err("Invalid category"),
}
}
}
pub fn count_accepted(sorting_system: &SortingSystem, first_workflow: &str) -> u64 {
sorting_system
.part_ratings
.iter()
.map(|rating| {
let mut current_workflow = first_workflow;
loop {
match current_workflow {
"A" => break rating.values().sum(),
"R" => break 0,
_ => {}
}
for condition in sorting_system.workflows.get(current_workflow).unwrap() {
match condition {
Condition::Less {
next_workflow,
category,
value,
} => {
if rating.get(category).unwrap() < value {
current_workflow = next_workflow;
break;
}
}
Condition::Greater {
next_workflow,
category,
value,
} => {
if rating.get(category).unwrap() > value {
current_workflow = next_workflow;
break;
}
}
Condition::None { next_workflow } => {
current_workflow = next_workflow;
break;
}
}
}
}
})
.sum()
}
pub fn count_all_accepted_combinations_recursively(
workflows: &rustc_hash::FxHashMap<String, smallvec::SmallVec<[Condition; 4]>>,
mut ranges: rustc_hash::FxHashMap<Category, (u64, u64)>,
current_workflow: &str,
) -> u64 {
match current_workflow {
"A" => {
return ranges
.values()
.map(|(start, end)| end - start + 1)
.product()
}
"R" => return 0,
_ => {}
}
workflows[current_workflow]
.iter()
.map(|condition| match condition {
Condition::Less {
next_workflow,
category,
value,
} => {
let &(start, end) = ranges.get(category).unwrap();
let within_bounds = (start, (value.saturating_sub(1)).min(end));
ranges.insert(*category, within_bounds);
let ranges_within_bounds = ranges.clone();
let outside_bounds = (*value.max(&start), end);
ranges.insert(*category, outside_bounds);
count_all_accepted_combinations_recursively(
workflows,
ranges_within_bounds,
next_workflow,
)
}
Condition::Greater {
next_workflow,
category,
value,
} => {
let &(start, end) = ranges.get(category).unwrap();
let within_bounds = ((value.saturating_add(1)).max(start), end);
ranges.insert(*category, within_bounds);
let ranges_within_bounds = ranges.clone();
let outside_bounds = (start, *value.min(&end));
ranges.insert(*category, outside_bounds);
count_all_accepted_combinations_recursively(
workflows,
ranges_within_bounds,
next_workflow,
)
}
Condition::None { next_workflow } => count_all_accepted_combinations_recursively(
workflows,
ranges.clone(),
next_workflow,
),
})
.sum()
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
const SAMPLE: &str = indoc! {r"
px{a<2006:qkq,m>2090:A,rfg}
pv{a>1716:R,A}
lnx{m>1548:A,A}
rfg{s<537:gd,x>2440:R,A}
qs{s>3448:A,lnx}
qkq{x<1416:A,crn}
crn{x>2662:A,R}
in{s<1351:px,qqz}
qqz{s>2770:qs,m<1801:hdj,R}
gd{a>3333:R,R}
hdj{m>838:A,pv}
{x=787,m=2655,a=1222,s=2876}
{x=1679,m=44,a=2067,s=496}
{x=2036,m=264,a=79,s=2244}
{x=2461,m=1339,a=466,s=291}
{x=2127,m=1623,a=2188,s=1013}
"};
#[test]
pub fn part1_example() {
assert_eq!(part1(SAMPLE), 19114);
}
#[test]
pub fn part2_example() {
assert_eq!(part2(SAMPLE), 167_409_079_868_000);
}
}