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
use crate::input::Input;
use std::cmp::Ordering;
use std::collections::{BTreeSet, BinaryHeap, HashMap, HashSet};
#[derive(Eq)]
struct Step {
name: char,
dependencies: HashSet<char>,
needed_by: BTreeSet<char>,
}
impl Step {
fn new(name: char) -> Self {
Self {
name,
dependencies: HashSet::new(),
needed_by: BTreeSet::new(),
}
}
}
impl Ord for Step {
fn cmp(&self, other: &Self) -> Ordering {
other.name.cmp(&self.name)
}
}
impl PartialOrd for Step {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Step {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
struct ParsedInput {
step_map: HashMap<char, Step>,
remaining_dependencies: HashMap<char, HashSet<char>>,
}
fn parse_input(input_string: &str) -> Result<ParsedInput, String> {
let mut step_map = HashMap::new();
let mut remaining_dependencies: HashMap<char, HashSet<char>> = HashMap::new();
for (line_index, line) in input_string.lines().enumerate() {
let line_number = line_index + 1;
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() != 10 {
return Err(format!("Invalid line: {}", line_number));
}
let step_name = parts[7]
.chars()
.next()
.ok_or(format!("Invalid line: {}", line_number))?;
let depends_on = parts[1]
.chars()
.next()
.ok_or(format!("Invalid line: {}", line_number))?;
let step = step_map
.entry(step_name)
.or_insert_with(|| Step::new(step_name));
step.dependencies.insert(depends_on);
remaining_dependencies
.entry(step_name)
.or_insert_with(HashSet::new)
.insert(depends_on);
step_map
.entry(depends_on)
.or_insert_with(|| Step::new(depends_on))
.needed_by
.insert(step_name);
}
Ok(ParsedInput {
step_map,
remaining_dependencies,
})
}
#[derive(Eq)]
struct Work {
name: char,
done_at_second: i32,
}
impl Work {
const fn new(name: char, done_at_second: i32) -> Self {
Self {
name,
done_at_second,
}
}
}
impl Ord for Work {
fn cmp(&self, other: &Self) -> Ordering {
other.done_at_second.cmp(&self.done_at_second)
}
}
impl PartialOrd for Work {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Work {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
pub fn solve(input: &mut Input) -> Result<String, String> {
const WORKERS: usize = 5;
const STEP_DURATION_BASE: i32 = 60;
let ParsedInput {
step_map,
mut remaining_dependencies,
} = parse_input(input.text)?;
if input.is_part_one() {
let mut queue: BinaryHeap<&Step> = BinaryHeap::new();
step_map
.values()
.filter(|step| step.dependencies.is_empty())
.for_each(|step| {
queue.push(step);
});
let mut visited: HashSet<char> = HashSet::new();
let mut result = String::new();
while let Some(step) = queue.pop() {
if visited.insert(step.name) {
result.push(step.name);
for needed_by_name in step.needed_by.iter().rev() {
let v = remaining_dependencies
.get_mut(needed_by_name)
.ok_or("Dependency not found")?;
v.remove(&step.name);
if v.is_empty() {
queue.push(&step_map[needed_by_name]);
};
}
}
}
Ok(result)
} else {
let mut work_queue: BinaryHeap<Work> = BinaryHeap::new();
let mut step_queue: BinaryHeap<&Step> = BinaryHeap::new();
step_map
.values()
.filter(|step| step.dependencies.is_empty())
.for_each(|step| {
step_queue.push(step);
});
while work_queue.len() < WORKERS && !step_queue.is_empty() {
let step = step_queue.pop().ok_or("No step to pop")?;
let done_at_time = STEP_DURATION_BASE + (1 + step.name as i32 - 'A' as i32);
work_queue.push(Work::new(step.name, done_at_time));
}
let mut visited: HashSet<char> = HashSet::new();
let mut result = String::new();
let mut latest_work_done_at = 0;
while let Some(work_done) = work_queue.pop() {
latest_work_done_at = work_done.done_at_second;
result.push(work_done.name);
visited.insert(work_done.name);
let step = &step_map[&work_done.name];
for needed_by_name in step.needed_by.iter().rev() {
let v = remaining_dependencies
.get_mut(needed_by_name)
.ok_or("Dependency not found")?;
v.remove(&step.name);
if v.is_empty() {
step_queue.push(&step_map[needed_by_name]);
};
}
while work_queue.len() < WORKERS && !step_queue.is_empty() {
let next_step = step_queue.pop().ok_or("No step to pop")?;
let next_step_done_at = work_done.done_at_second
+ STEP_DURATION_BASE
+ (1 + next_step.name as i32 - 'A' as i32);
work_queue.push(Work::new(next_step.name, next_step_done_at));
}
}
Ok(latest_work_done_at.to_string())
}
}
#[test]
fn tests() {
use crate::input::{test_part_one, test_part_two};
test_part_one!(
"Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin."
=> "CABDFE".to_string()
);
test_part_one!(
"Step B must be finished before step A can begin.
Step C must be finished before step A can begin."
=> "BCA".to_string()
);
test_part_one!(
"Step C must be finished before step A can begin.
Step B must be finished before step A can begin."
=> "BCA".to_string()
);
let input = include_str!("day07_input.txt");
test_part_one!(
input => "OUGLTKDJVBRMIXSACWYPEQNHZF".to_string()
);
test_part_two!(input => "929".to_string());
}