rustfmt and fix clippy warnings

This commit is contained in:
Tyler Hallada 2019-01-03 01:10:10 -05:00
parent f4392d62c0
commit 0f2e4bdf47
2 changed files with 50 additions and 27 deletions

View File

@ -1,23 +1,25 @@
extern crate regex;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::fmt;
use std::collections::HashMap;
use regex::{Regex, Captures};
use regex::{Captures, Regex};
const INPUT: &str = "inputs/7.txt";
#[derive(Debug, Clone, PartialEq)]
struct MalformedInstruction {
details: String
details: String,
}
impl MalformedInstruction {
fn new(msg: &str) -> MalformedInstruction {
MalformedInstruction{ details: msg.to_string() }
MalformedInstruction {
details: msg.to_string(),
}
}
}
@ -43,21 +45,26 @@ fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box
lazy_static! {
static ref INSTRUCTION_REGEX: Regex = Regex::new(
r"Step (?P<dependency>\w) must be finished before step (?P<step>\w) can begin."
).unwrap();
)
.unwrap();
}
let file = File::open(filename)?;
for (index, line) in BufReader::new(file).lines().enumerate() {
for line in BufReader::new(file).lines() {
match INSTRUCTION_REGEX.captures(&line?) {
Some(captures) => {
let step = get_captured_field(&captures, "step")?.parse()?;
let dependency: String = get_captured_field(&captures, "dependency")?.parse()?;
instructions.entry(dependency.clone()).or_insert(Vec::new());
let dependencies = instructions.entry(step).or_insert(Vec::new());
instructions
.entry(dependency.clone())
.or_insert_with(Vec::new);
let dependencies = instructions.entry(step).or_insert_with(Vec::new);
dependencies.push(dependency);
},
None => return Err(Box::new(MalformedInstruction {
details: "Malformed instruction line, no fields could be found".to_string()
})),
}
None => {
return Err(Box::new(MalformedInstruction {
details: "Malformed instruction line, no fields could be found".to_string(),
}))
}
};
}
Ok(instructions)
@ -66,9 +73,12 @@ fn read_instructions(filename: &str) -> Result<HashMap<String, Vec<String>>, Box
fn get_captured_field(captures: &Captures, field: &str) -> Result<String, Box<Error>> {
match captures.name(field) {
Some(capture) => Ok(String::from(capture.as_str())),
None => return Err(Box::new(MalformedInstruction {
details: format!("Malformed instruction line, field {} could not be found", field)
}))
None => Err(Box::new(MalformedInstruction {
details: format!(
"Malformed instruction line, field {} could not be found",
field
),
})),
}
}
@ -77,18 +87,19 @@ fn get_step_sequence(instructions: &mut HashMap<String, Vec<String>>) -> String
loop {
let mut available: Vec<String> = instructions
.iter()
.filter(|(_, dependencies)| dependencies.len() == 0)
.filter(|(_, dependencies)| dependencies.is_empty())
.map(|(step, _)| step.clone())
.collect();
if available.len() == 0 { break; }
if available.is_empty() {
break;
}
available.sort();
available.reverse();
let next = available.pop().unwrap();
instructions.remove(&next);
for dependencies in instructions.values_mut() {
match dependencies.iter().position(|d| *d == next) {
Some(index) => { dependencies.remove(index); },
None => ()
if let Some(index) = dependencies.iter().position(|d| *d == next) {
dependencies.remove(index);
}
}
sequence.push(next);
@ -110,9 +121,14 @@ mod tests {
("C".to_string(), vec![]),
("B".to_string(), vec!["A".to_string()]),
("D".to_string(), vec!["A".to_string()]),
("E".to_string(), vec!["B".to_string(), "D".to_string(), "F".to_string()]),
].iter().cloned().collect();
(
"E".to_string(),
vec!["B".to_string(), "D".to_string(), "F".to_string()],
),
]
.iter()
.cloned()
.collect();
assert_eq!(read_instructions(TEST_INPUT).unwrap(), expected);
}
@ -124,9 +140,14 @@ mod tests {
("C".to_string(), vec![]),
("B".to_string(), vec!["A".to_string()]),
("D".to_string(), vec!["A".to_string()]),
("E".to_string(), vec!["B".to_string(), "D".to_string(), "F".to_string()]),
].iter().cloned().collect();
(
"E".to_string(),
vec!["B".to_string(), "D".to_string(), "F".to_string()],
),
]
.iter()
.cloned()
.collect();
assert_eq!(get_step_sequence(&mut instructions), "CABDFE");
}
}

View File

@ -1,3 +1,5 @@
#![warn(clippy)]
#[macro_use]
extern crate lazy_static;