|
@@ -2,45 +2,22 @@ extern crate regex;
|
2
|
2
|
|
3
|
3
|
use std::collections::HashMap;
|
4
|
4
|
use std::error::Error;
|
5
|
|
-use std::fmt;
|
6
|
5
|
use std::fs::File;
|
7
|
6
|
use std::io::{BufRead, BufReader};
|
|
7
|
+use std::result;
|
8
|
8
|
|
9
|
9
|
use regex::{Captures, Regex};
|
10
|
10
|
|
11
|
|
-const INPUT: &str = "inputs/7.txt";
|
12
|
|
-
|
13
|
|
-#[derive(Debug, Clone, PartialEq)]
|
14
|
|
-struct MalformedInstruction {
|
15
|
|
- details: String,
|
16
|
|
-}
|
17
|
|
-
|
18
|
|
-impl MalformedInstruction {
|
19
|
|
- fn new(msg: &str) -> MalformedInstruction {
|
20
|
|
- MalformedInstruction {
|
21
|
|
- details: msg.to_string(),
|
22
|
|
- }
|
23
|
|
- }
|
24
|
|
-}
|
|
11
|
+type Result<T> = result::Result<T, Box<Error>>;
|
25
|
12
|
|
26
|
|
-impl fmt::Display for MalformedInstruction {
|
27
|
|
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
28
|
|
- write!(f, "{}", self.details)
|
29
|
|
- }
|
30
|
|
-}
|
31
|
|
-
|
32
|
|
-impl Error for MalformedInstruction {
|
33
|
|
- fn description(&self) -> &str {
|
34
|
|
- &self.details
|
35
|
|
- }
|
36
|
|
-}
|
|
13
|
+const INPUT: &str = "inputs/7.txt";
|
37
|
14
|
|
38
|
|
-pub fn solve_part1() -> Result<String, Box<Error>> {
|
|
15
|
+pub fn solve_part1() -> Result<String> {
|
39
|
16
|
let mut instructions = read_instructions(INPUT)?;
|
40
|
17
|
Ok(get_step_sequence(&mut instructions))
|
41
|
18
|
}
|
42
|
19
|
|
43
|
|
-fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box<Error>> {
|
|
20
|
+fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>> {
|
44
|
21
|
let mut instructions: HashMap<String, Vec<String>> = HashMap::new();
|
45
|
22
|
lazy_static! {
|
46
|
23
|
static ref INSTRUCTION_REGEX: Regex = Regex::new(
|
|
@@ -61,24 +38,22 @@ fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box
|
61
|
38
|
dependencies.push(dependency);
|
62
|
39
|
}
|
63
|
40
|
None => {
|
64
|
|
- return Err(Box::new(MalformedInstruction {
|
65
|
|
- details: "Malformed instruction line, no fields could be found".to_string(),
|
66
|
|
- }))
|
|
41
|
+ return Err(From::from(
|
|
42
|
+ "Malformed instruction line, no fields could be found",
|
|
43
|
+ ))
|
67
|
44
|
}
|
68
|
45
|
};
|
69
|
46
|
}
|
70
|
47
|
Ok(instructions)
|
71
|
48
|
}
|
72
|
49
|
|
73
|
|
-fn get_captured_field(captures: &Captures, field: &str) -> Result<String, Box<Error>> {
|
|
50
|
+fn get_captured_field(captures: &Captures, field: &str) -> Result<String> {
|
74
|
51
|
match captures.name(field) {
|
75
|
52
|
Some(capture) => Ok(String::from(capture.as_str())),
|
76
|
|
- None => Err(Box::new(MalformedInstruction {
|
77
|
|
- details: format!(
|
78
|
|
- "Malformed instruction line, field {} could not be found",
|
79
|
|
- field
|
80
|
|
- ),
|
81
|
|
- })),
|
|
53
|
+ None => Err(From::from(format!(
|
|
54
|
+ "Malformed instruction line, field {} could not be found",
|
|
55
|
+ field
|
|
56
|
+ ))),
|
82
|
57
|
}
|
83
|
58
|
}
|
84
|
59
|
|