|
@@ -1,23 +1,25 @@
|
1
|
1
|
extern crate regex;
|
2
|
2
|
|
|
3
|
+use std::collections::HashMap;
|
3
|
4
|
use std::error::Error;
|
|
5
|
+use std::fmt;
|
4
|
6
|
use std::fs::File;
|
5
|
7
|
use std::io::{BufRead, BufReader};
|
6
|
|
-use std::fmt;
|
7
|
|
-use std::collections::HashMap;
|
8
|
8
|
|
9
|
|
-use regex::{Regex, Captures};
|
|
9
|
+use regex::{Captures, Regex};
|
10
|
10
|
|
11
|
11
|
const INPUT: &str = "inputs/7.txt";
|
12
|
12
|
|
13
|
13
|
#[derive(Debug, Clone, PartialEq)]
|
14
|
14
|
struct MalformedInstruction {
|
15
|
|
- details: String
|
|
15
|
+ details: String,
|
16
|
16
|
}
|
17
|
17
|
|
18
|
18
|
impl MalformedInstruction {
|
19
|
19
|
fn new(msg: &str) -> MalformedInstruction {
|
20
|
|
- MalformedInstruction{ details: msg.to_string() }
|
|
20
|
+ MalformedInstruction {
|
|
21
|
+ details: msg.to_string(),
|
|
22
|
+ }
|
21
|
23
|
}
|
22
|
24
|
}
|
23
|
25
|
|
|
@@ -43,21 +45,26 @@ fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box
|
43
|
45
|
lazy_static! {
|
44
|
46
|
static ref INSTRUCTION_REGEX: Regex = Regex::new(
|
45
|
47
|
r"Step (?P<dependency>\w) must be finished before step (?P<step>\w) can begin."
|
46
|
|
- ).unwrap();
|
|
48
|
+ )
|
|
49
|
+ .unwrap();
|
47
|
50
|
}
|
48
|
51
|
let file = File::open(filename)?;
|
49
|
|
- for (index, line) in BufReader::new(file).lines().enumerate() {
|
|
52
|
+ for line in BufReader::new(file).lines() {
|
50
|
53
|
match INSTRUCTION_REGEX.captures(&line?) {
|
51
|
54
|
Some(captures) => {
|
52
|
55
|
let step = get_captured_field(&captures, "step")?.parse()?;
|
53
|
56
|
let dependency: String = get_captured_field(&captures, "dependency")?.parse()?;
|
54
|
|
- instructions.entry(dependency.clone()).or_insert(Vec::new());
|
55
|
|
- let dependencies = instructions.entry(step).or_insert(Vec::new());
|
|
57
|
+ instructions
|
|
58
|
+ .entry(dependency.clone())
|
|
59
|
+ .or_insert_with(Vec::new);
|
|
60
|
+ let dependencies = instructions.entry(step).or_insert_with(Vec::new);
|
56
|
61
|
dependencies.push(dependency);
|
57
|
|
- },
|
58
|
|
- None => return Err(Box::new(MalformedInstruction {
|
59
|
|
- details: "Malformed instruction line, no fields could be found".to_string()
|
60
|
|
- })),
|
|
62
|
+ }
|
|
63
|
+ None => {
|
|
64
|
+ return Err(Box::new(MalformedInstruction {
|
|
65
|
+ details: "Malformed instruction line, no fields could be found".to_string(),
|
|
66
|
+ }))
|
|
67
|
+ }
|
61
|
68
|
};
|
62
|
69
|
}
|
63
|
70
|
Ok(instructions)
|
|
@@ -66,9 +73,12 @@ fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box
|
66
|
73
|
fn get_captured_field(captures: &Captures, field: &str) -> Result<String, Box<Error>> {
|
67
|
74
|
match captures.name(field) {
|
68
|
75
|
Some(capture) => Ok(String::from(capture.as_str())),
|
69
|
|
- None => return Err(Box::new(MalformedInstruction {
|
70
|
|
- details: format!("Malformed instruction line, field {} could not be found", field)
|
71
|
|
- }))
|
|
76
|
+ None => Err(Box::new(MalformedInstruction {
|
|
77
|
+ details: format!(
|
|
78
|
+ "Malformed instruction line, field {} could not be found",
|
|
79
|
+ field
|
|
80
|
+ ),
|
|
81
|
+ })),
|
72
|
82
|
}
|
73
|
83
|
}
|
74
|
84
|
|
|
@@ -77,18 +87,19 @@ fn get_step_sequence(instructions: &mut HashMap<String, Vec<String>>) -> String
|
77
|
87
|
loop {
|
78
|
88
|
let mut available: Vec<String> = instructions
|
79
|
89
|
.iter()
|
80
|
|
- .filter(|(_, dependencies)| dependencies.len() == 0)
|
|
90
|
+ .filter(|(_, dependencies)| dependencies.is_empty())
|
81
|
91
|
.map(|(step, _)| step.clone())
|
82
|
92
|
.collect();
|
83
|
|
- if available.len() == 0 { break; }
|
|
93
|
+ if available.is_empty() {
|
|
94
|
+ break;
|
|
95
|
+ }
|
84
|
96
|
available.sort();
|
85
|
97
|
available.reverse();
|
86
|
98
|
let next = available.pop().unwrap();
|
87
|
99
|
instructions.remove(&next);
|
88
|
100
|
for dependencies in instructions.values_mut() {
|
89
|
|
- match dependencies.iter().position(|d| *d == next) {
|
90
|
|
- Some(index) => { dependencies.remove(index); },
|
91
|
|
- None => ()
|
|
101
|
+ if let Some(index) = dependencies.iter().position(|d| *d == next) {
|
|
102
|
+ dependencies.remove(index);
|
92
|
103
|
}
|
93
|
104
|
}
|
94
|
105
|
sequence.push(next);
|
|
@@ -110,9 +121,14 @@ mod tests {
|
110
|
121
|
("C".to_string(), vec![]),
|
111
|
122
|
("B".to_string(), vec!["A".to_string()]),
|
112
|
123
|
("D".to_string(), vec!["A".to_string()]),
|
113
|
|
- ("E".to_string(), vec!["B".to_string(), "D".to_string(), "F".to_string()]),
|
114
|
|
-
|
115
|
|
- ].iter().cloned().collect();
|
|
124
|
+ (
|
|
125
|
+ "E".to_string(),
|
|
126
|
+ vec!["B".to_string(), "D".to_string(), "F".to_string()],
|
|
127
|
+ ),
|
|
128
|
+ ]
|
|
129
|
+ .iter()
|
|
130
|
+ .cloned()
|
|
131
|
+ .collect();
|
116
|
132
|
assert_eq!(read_instructions(TEST_INPUT).unwrap(), expected);
|
117
|
133
|
}
|
118
|
134
|
|
|
@@ -124,9 +140,14 @@ mod tests {
|
124
|
140
|
("C".to_string(), vec![]),
|
125
|
141
|
("B".to_string(), vec!["A".to_string()]),
|
126
|
142
|
("D".to_string(), vec!["A".to_string()]),
|
127
|
|
- ("E".to_string(), vec!["B".to_string(), "D".to_string(), "F".to_string()]),
|
128
|
|
-
|
129
|
|
- ].iter().cloned().collect();
|
|
143
|
+ (
|
|
144
|
+ "E".to_string(),
|
|
145
|
+ vec!["B".to_string(), "D".to_string(), "F".to_string()],
|
|
146
|
+ ),
|
|
147
|
+ ]
|
|
148
|
+ .iter()
|
|
149
|
+ .cloned()
|
|
150
|
+ .collect();
|
130
|
151
|
assert_eq!(get_step_sequence(&mut instructions), "CABDFE");
|
131
|
152
|
}
|
132
|
153
|
}
|