-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd19.rs
More file actions
244 lines (211 loc) Β· 5.92 KB
/
d19.rs
File metadata and controls
244 lines (211 loc) Β· 5.92 KB
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
use crate::Day;
pub struct Day19 {}
impl Day for Day19 {
fn year(&self) -> u16 {
2023
}
fn day(&self) -> u8 {
19
}
fn part_one(&self) -> String {
let input = self.read_default_input();
let input_parts: Vec<&str> = input.split("\n\n").collect();
let workflows: Vec<Workflow> = input_parts[0].split("\n").map(Workflow::parse).collect();
let parts: Vec<Part> = input_parts[1].split("\n").map(Part::parse).collect();
let initial_workflow = find_workflow("in", &workflows);
let mut accepted_parts: Vec<&Part> = vec![];
for part in &parts {
let mut next_workflow = initial_workflow;
while next_workflow.is_some() {
match next_workflow.unwrap().run(part) {
Destination::Workflow(workflow) => {
next_workflow = Some(find_workflow(workflow, &workflows).unwrap())
}
Destination::Outcome(outcome) => {
if *outcome == Outcome::Accepted {
accepted_parts.push(part);
}
next_workflow = None;
}
}
}
}
let res = accepted_parts
.into_iter()
.fold(0u64, |acc, x| acc + x.rating() as u64);
res.to_string()
}
fn part_two(&self) -> String {
"-".to_owned()
}
}
#[derive(Debug)]
enum Category {
X,
M,
A,
S,
}
impl Category {
fn parse(input: char) -> Self {
match input {
'x' => Self::X,
'm' => Self::M,
'a' => Self::A,
's' => Self::S,
_ => panic!("Unknown category {}", input),
}
}
}
#[derive(Debug)]
struct Part {
x: u16,
m: u16,
a: u16,
s: u16,
}
impl Part {
fn parse(input: &str) -> Self {
let input = &input[1..input.len() - 1];
let mut part = Self {
x: 0,
m: 0,
a: 0,
s: 0,
};
for mut category_pair in input.split(',').map(|it| it.split('=')) {
let category = category_pair.next().unwrap();
let value = category_pair.next().unwrap().parse::<u16>().unwrap();
match category {
"x" => part.x = value,
"m" => part.m = value,
"a" => part.a = value,
"s" => part.s = value,
_ => panic!("Unknown category {} for part", category),
}
}
part
}
fn rating(&self) -> u16 {
self.x + self.m + self.a + self.s
}
}
#[derive(Debug)]
enum Cmp {
Lte,
Gte,
}
impl Cmp {
fn parse(input: char) -> Self {
match input {
'>' => Self::Gte,
'<' => Self::Lte,
_ => panic!("Unknown comparison {}", input),
}
}
fn evaluate(&self, left: u16, right: u16) -> bool {
match self {
Cmp::Lte => left < right,
Cmp::Gte => left > right,
}
}
}
#[derive(Debug, Eq, PartialEq)]
enum Outcome {
Accepted,
Rejected,
}
#[derive(Debug)]
enum Destination {
Workflow(String),
Outcome(Outcome),
}
impl Destination {
fn parse(input: &str) -> Self {
match input {
"A" => Self::Outcome(Outcome::Accepted),
"R" => Self::Outcome(Outcome::Rejected),
workflow => Self::Workflow(String::from(workflow)),
}
}
}
#[derive(Debug)]
enum Rule {
Value {
category: Category,
cmp: Cmp,
value: u16,
destination: Destination,
},
Destination {
destination: Destination,
},
}
impl Rule {
fn parse(input: &str) -> Self {
match input.chars().position(|x| x == '<' || x == '>') {
Some(cmp_position) => {
let mut parts = input.split(&['<', '>', ':']);
Self::Value {
category: Category::parse(parts.next().unwrap().chars().next().unwrap()),
cmp: Cmp::parse(input.chars().nth(cmp_position).unwrap()),
value: parts.next().unwrap().parse::<u16>().unwrap(),
destination: Destination::parse(parts.next().unwrap()),
}
}
None => Self::Destination {
destination: Destination::parse(input),
},
}
}
fn apply(&self, part: &Part) -> Option<&Destination> {
match self {
Rule::Value {
category,
cmp,
value,
destination,
} => {
let eval = match category {
Category::X => cmp.evaluate(part.x, *value),
Category::M => cmp.evaluate(part.m, *value),
Category::A => cmp.evaluate(part.a, *value),
Category::S => cmp.evaluate(part.s, *value),
};
if eval { Some(destination) } else { None }
}
Rule::Destination { destination } => Some(destination),
}
}
}
#[derive(Debug)]
struct Workflow {
name: String,
rules: Vec<Rule>,
}
impl Workflow {
fn parse(input: &str) -> Self {
let mut p = input.split("{");
let name = p.next().unwrap();
let rules_raw = p.next().unwrap();
let rules: Vec<Rule> = rules_raw[0..rules_raw.len() - 1]
.split(",")
.map(Rule::parse)
.collect();
Self {
name: String::from(name),
rules,
}
}
fn run(&self, part: &Part) -> &Destination {
for rule in &self.rules {
if let Some(destination) = rule.apply(part) {
return destination;
}
}
unreachable!("No rules were matched");
}
}
fn find_workflow<'a>(name: &'a str, workflows: &'a [Workflow]) -> Option<&'a Workflow> {
workflows.iter().find(|wf| wf.name == name)
}