This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsolution.rs
162 lines (143 loc) · 4.64 KB
/
solution.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
fn main() {
let input = include_str!("./data/inputs/day_19.txt");
println!("Part A: \x1b[1m{}\x1b[0m", part_a(input).unwrap());
println!("Part B: \x1b[1m{}\x1b[0m", part_b(input).unwrap());
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Workflow {
name: String,
steps: Vec<WorkflowStep>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct ConditionalWorkflowStep {
category: Category,
comparison: Comparison,
value: u32,
send_to_workflow: String,
}
impl WorkflowStep {
fn get_next_workflow(&self, part: &Part) -> Option<String> {
match self {
WorkflowStep::Conditional(conditional) => {
let value_to_compare = match conditional.category {
Category::X => part.x,
Category::M => part.m,
Category::A => part.a,
Category::S => part.s,
};
let send = if conditional.comparison == Comparison::Greater {
value_to_compare > conditional.value
} else {
value_to_compare < conditional.value
};
if send {
Some(conditional.send_to_workflow.clone())
} else {
None
}
}
WorkflowStep::Unconditional(u) => Some(u.to_string()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum WorkflowStep {
Conditional(ConditionalWorkflowStep),
Unconditional(String),
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum Comparison {
Greater,
Lesser,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum Category {
X,
M,
A,
S,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
struct Part {
x: u32,
m: u32,
a: u32,
s: u32,
}
pub fn part_a(input: &str) -> Option<u32> {
let (workflow_string, parts_string) = input.split_once("\n\n").unwrap();
let mut workflows = vec![];
for workflow in workflow_string.lines() {
let (name, rest) = workflow.split_once('{').unwrap();
let steps = rest.strip_suffix('}').unwrap().split(',');
let mut step_list = vec![];
for step in steps {
if step.contains(':') {
let (comparison, cmp_symbol) = if step.contains('<') {
(Comparison::Lesser, "<")
} else {
(Comparison::Greater, ">")
};
let (category_string, rest) = step.split_once(cmp_symbol).unwrap();
let category = match category_string {
"x" => Category::X,
"m" => Category::M,
"a" => Category::A,
"s" => Category::S,
_ => unreachable!(),
};
let (value_string, next_workflow) = rest.split_once(':').unwrap();
let step = ConditionalWorkflowStep {
category,
comparison,
value: value_string.parse().unwrap(),
send_to_workflow: next_workflow.to_string(),
};
step_list.push(WorkflowStep::Conditional(step));
} else {
step_list.push(WorkflowStep::Unconditional(step.to_string()));
}
}
let workflow = Workflow {
name: name.to_string(),
steps: step_list,
};
workflows.push(workflow)
}
let mut parts = vec![];
for part in parts_string.lines() {
let stripped_part = part.strip_prefix('{').unwrap().strip_suffix('}').unwrap();
let attributes = stripped_part
.split(',')
.map(|attr| attr.split_once('=').unwrap().1.parse().unwrap())
.collect::<Vec<_>>();
parts.push(Part {
x: attributes[0],
m: attributes[1],
a: attributes[2],
s: attributes[3],
});
}
let mut sum = 0;
for part in parts {
let mut current_workflow_name = String::from("in");
while !["A", "R"].contains(¤t_workflow_name.as_str()) {
let current_workflow = workflows
.iter()
.find(|w| w.name == current_workflow_name)
.unwrap();
current_workflow_name = current_workflow
.steps
.iter()
.find_map(|step| step.get_next_workflow(&part))
.unwrap();
}
if current_workflow_name == "A" {
sum += part.x + part.m + part.a + part.s;
}
}
Some(sum)
}
pub fn part_b(_input: &str) -> Option<u64> {
None
}