-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.rs
157 lines (123 loc) · 4.11 KB
/
day7.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
//! [Day 7: The Sum of Its Parts](https://adventofcode.com/2018/day/7)
use rustc_hash::{FxHashMap, FxHashSet};
struct Puzzle {
deps: FxHashMap<char, FxHashSet<char>>,
}
impl Puzzle {
fn new() -> Self {
Self {
deps: FxHashMap::default(),
}
}
/// Get the puzzle input.
fn configure(&mut self, data: &str) {
for line in data.lines() {
let line: Vec<_> = line.split_ascii_whitespace().collect();
// Step A must be finished before step B can begin.
let a = line[1].chars().next().unwrap();
let b = line[7].chars().next().unwrap();
self.deps.entry(b).or_default().insert(a);
self.deps.entry(a).or_default();
}
}
/// Solve part one.
fn part1(&self) -> String {
let mut result = String::new();
let n = self.deps.len();
let mut deps = self.deps.clone();
let mut steps: Vec<_> = deps.keys().copied().collect();
steps.sort_unstable();
while result.len() < n {
for (i, step) in steps.iter().enumerate() {
if deps[step].is_empty() {
// first step to have no dependency, in alphabetical order
result.push(*step);
// remove it from the dependencies list of other steps
for k in deps.values_mut() {
k.remove(step);
}
steps.remove(i);
break;
}
}
}
result
}
fn solve_part2(&self, nb_workers: usize, base_delay: u32) -> u32 {
let mut processed = 0;
let n = self.deps.len();
let mut deps = self.deps.clone();
let mut steps: Vec<_> = deps.keys().copied().collect();
steps.sort_unstable();
let mut workers = vec![('_', 0u32); nb_workers];
let mut seconds = 0;
loop {
for (worker_step, worker_timer) in &mut workers {
if *worker_timer == 0 {
// worker is doing nothing
continue;
}
*worker_timer -= 1;
if *worker_timer == 0 {
// process of step has finished
processed += 1;
// remove it from the dependencies list of other steps
for k in deps.values_mut() {
k.remove(worker_step);
}
}
}
for (worker_step, worker_timer) in &mut workers {
if *worker_timer != 0 {
// worker is busy
continue;
}
// find a step to work on
for (i, step) in steps.iter().enumerate() {
if deps[step].is_empty() {
// first step with no dependency in alphabetical order
// affect it to the current worker
*worker_step = *step;
*worker_timer = (*step as u32) - ('A' as u32) + 1 + base_delay;
// remove it to the waiting list
steps.remove(i);
break;
}
}
}
if processed >= n {
break;
}
seconds += 1;
}
seconds
}
/// Solve part two.
fn part2(&self) -> u32 {
self.solve_part2(5, 60)
}
}
fn main() {
let args = aoc::parse_args();
let mut puzzle = Puzzle::new();
puzzle.configure(&args.input);
println!("{}", puzzle.part1());
println!("{}", puzzle.part2());
}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test01() {
let mut puzzle = Puzzle::new();
puzzle.configure(&aoc::load_input_data("test.txt"));
assert_eq!(puzzle.part1(), "CABDFE");
}
#[test]
fn test02() {
let mut puzzle = Puzzle::new();
puzzle.configure(&aoc::load_input_data("test.txt"));
assert_eq!(puzzle.solve_part2(2, 0), 15);
}
}