diff --git a/src/day7.rs b/src/day7.rs index 9750400..f3c3af7 100644 --- a/src/day7.rs +++ b/src/day7.rs @@ -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>, Box lazy_static! { static ref INSTRUCTION_REGEX: Regex = Regex::new( r"Step (?P\w) must be finished before step (?P\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>, Box fn get_captured_field(captures: &Captures, field: &str) -> Result> { 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 loop { let mut available: Vec = 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"); } } diff --git a/src/main.rs b/src/main.rs index 63a32d0..e2cfc30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy)] + #[macro_use] extern crate lazy_static;