generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.rs
350 lines (294 loc) · 9.14 KB
/
19.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use std::{cmp::Ordering, collections::HashMap, ops::Range};
advent_of_code::solution!(19);
pub fn part_one(input: &str) -> Option<u32> {
let mut input_iterator = input.split("\n\n");
let workflow_map = WorkflowMap::from_input(input_iterator.next().unwrap());
let sum = input_iterator
.next()
.unwrap()
.lines()
.map(Object::from_line)
.filter_map(|object| {
if workflow_map.accepts(&object) {
Some(object.x + object.m + object.a + object.s)
} else {
None
}
})
.sum();
Some(sum)
}
pub fn part_two(input: &str) -> Option<u64> {
let sum = WorkflowMap::from_input(input.split("\n\n").next().unwrap())
.accepted_object_ranges()
.iter()
.map(|object_range| object_range.count())
.sum();
Some(sum)
}
struct WorkflowMap<'a> {
workflows: HashMap<&'a str, Workflow<'a>>,
}
impl WorkflowMap<'_> {
fn from_input(input: &str) -> WorkflowMap {
WorkflowMap {
workflows: input
.lines()
.map(Workflow::from_line)
.map(|workflow| (workflow.name, workflow))
.collect(),
}
}
fn accepts(&self, object: &Object) -> bool {
let mut command = &Command::Forward("in");
while let Command::Forward(workflow_name) = command {
command = self.next_command(workflow_name, object);
}
match command {
Command::Accept => true,
Command::Reject => false,
_ => panic!("Invalid command"),
}
}
fn next_command(&self, workflow_name: &str, object: &Object) -> &Command {
self.workflow(workflow_name).next_command(object)
}
fn workflow(&self, name: &str) -> &Workflow {
self.workflows.get(name).unwrap()
}
fn accepted_object_ranges(&self) -> Vec<ObjectRange> {
self.accepted_object_ranges_recursive(&Command::Forward("in"))
.unwrap()
}
fn accepted_object_ranges_recursive(&self, command: &Command) -> Option<Vec<ObjectRange>> {
match command {
Command::Reject => None,
Command::Accept => Some(vec![ObjectRange::new()]),
Command::Forward(workflow_name) => {
let workflow = self.workflow(workflow_name);
let mut object_ranges =
self.accepted_object_ranges_recursive(&workflow.final_command);
for rule in workflow.rules.iter().rev() {
if let Some(object_ranges) = &mut object_ranges {
for object_range in object_ranges {
object_range.subtract(&rule.condition);
}
}
if let Some(mut new_object_ranges) =
self.accepted_object_ranges_recursive(&rule.command)
{
for object_range in new_object_ranges.iter_mut() {
object_range.add(&rule.condition);
}
if let Some(object_ranges) = &mut object_ranges {
object_ranges.extend(new_object_ranges);
} else {
object_ranges = Some(new_object_ranges);
}
}
}
object_ranges
}
}
}
}
struct Workflow<'a> {
name: &'a str,
rules: Vec<Rule<'a>>,
final_command: Command<'a>,
}
impl Workflow<'_> {
fn from_line(line: &str) -> Workflow {
let mut line_iterator = line.split('{');
let name = line_iterator.next().unwrap();
let rules_string = line_iterator.next().unwrap();
let mut rules_iterator = rules_string[..rules_string.len() - 1].split(',');
let final_command = Command::from_string(rules_iterator.next_back().unwrap());
let mut rules: Vec<_> = rules_iterator.map(Rule::from_string).collect();
// Optimization that removes unnecessary rules
while rules
.last()
.map_or(false, |rule| rule.command == final_command)
{
rules.pop();
}
Workflow {
name,
rules,
final_command,
}
}
fn next_command(&self, object: &Object) -> &Command {
self.rules
.iter()
.find(|rule| rule.applies_to(object))
.map_or(&self.final_command, |rule| &rule.command)
}
}
struct Rule<'a> {
condition: Condition,
command: Command<'a>,
}
impl Rule<'_> {
fn from_string(string: &str) -> Rule {
let mut string_iterator = string.split(':');
Rule {
condition: Condition::from_string(string_iterator.next().unwrap()),
command: Command::from_string(string_iterator.next().unwrap()),
}
}
fn applies_to(&self, object: &Object) -> bool {
self.condition.applies_to(object)
}
}
struct Condition {
property: Property,
ordering: Ordering,
cmp_value: u32,
}
impl Condition {
fn from_string(string: &str) -> Condition {
let property = match &string[0..1] {
"x" => Property::X,
"m" => Property::M,
"a" => Property::A,
"s" => Property::S,
_ => panic!("Invalid property {}", string),
};
let ordering = match &string[1..2] {
"<" => Ordering::Less,
">" => Ordering::Greater,
_ => panic!("Invalid ordering"),
};
Condition {
property,
ordering,
cmp_value: string[2..].parse().unwrap(),
}
}
fn applies_to(&self, object: &Object) -> bool {
self.ordering == object.value(&self.property).cmp(&self.cmp_value)
}
}
enum Property {
X,
M,
A,
S,
}
#[derive(PartialEq)]
enum Command<'a> {
Accept,
Reject,
Forward(&'a str),
}
impl Command<'_> {
fn from_string(string: &str) -> Command {
match string {
"A" => Command::Accept,
"R" => Command::Reject,
_ => Command::Forward(string),
}
}
}
struct Object {
x: u32,
m: u32,
a: u32,
s: u32,
}
impl Object {
fn from_line(line: &str) -> Object {
let mut line_iterator = line[1..line.len() - 1].split(',');
Object {
x: line_iterator.next().unwrap()[2..].parse().unwrap(),
m: line_iterator.next().unwrap()[2..].parse().unwrap(),
a: line_iterator.next().unwrap()[2..].parse().unwrap(),
s: line_iterator.next().unwrap()[2..].parse().unwrap(),
}
}
fn value(&self, property: &Property) -> u32 {
match property {
Property::X => self.x,
Property::M => self.m,
Property::A => self.a,
Property::S => self.s,
}
}
}
struct ObjectRange {
x: Range<u32>,
m: Range<u32>,
a: Range<u32>,
s: Range<u32>,
}
impl ObjectRange {
fn new() -> ObjectRange {
ObjectRange {
x: (1..4001),
m: (1..4001),
a: (1..4001),
s: (1..4001),
}
}
fn range_mut(&mut self, property: &Property) -> &mut Range<u32> {
match property {
Property::X => &mut self.x,
Property::M => &mut self.m,
Property::A => &mut self.a,
Property::S => &mut self.s,
}
}
fn subtract(&mut self, condition: &Condition) {
let range = self.range_mut(&condition.property);
match condition.ordering {
Ordering::Less => {
if range.start < condition.cmp_value {
range.start = condition.cmp_value;
}
}
Ordering::Greater => {
if range.end > condition.cmp_value + 1 {
range.end = condition.cmp_value + 1;
}
}
_ => panic!("Invalid ordering"),
}
}
fn add(&mut self, condition: &Condition) {
let range = self.range_mut(&condition.property);
match condition.ordering {
Ordering::Less => {
if range.end > condition.cmp_value {
range.end = condition.cmp_value;
}
}
Ordering::Greater => {
if range.start <= condition.cmp_value {
range.start = condition.cmp_value + 1;
}
}
_ => panic!("Invalid ordering"),
}
if range.start.cmp(&condition.cmp_value) != condition.ordering {
range.start = condition.cmp_value;
}
}
fn count(&self) -> u64 {
self.x.len() as u64 * self.m.len() as u64 * self.a.len() as u64 * self.s.len() as u64
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(19114));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(167409079868000));
}
}