rustfmt and fix clippy warnings
This commit is contained in:
parent
f4392d62c0
commit
0f2e4bdf47
75
src/day7.rs
75
src/day7.rs
@ -1,23 +1,25 @@
|
|||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::fmt;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader};
|
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";
|
const INPUT: &str = "inputs/7.txt";
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
struct MalformedInstruction {
|
struct MalformedInstruction {
|
||||||
details: String
|
details: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MalformedInstruction {
|
impl MalformedInstruction {
|
||||||
fn new(msg: &str) -> 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! {
|
lazy_static! {
|
||||||
static ref INSTRUCTION_REGEX: Regex = Regex::new(
|
static ref INSTRUCTION_REGEX: Regex = Regex::new(
|
||||||
r"Step (?P<dependency>\w) must be finished before step (?P<step>\w) can begin."
|
r"Step (?P<dependency>\w) must be finished before step (?P<step>\w) can begin."
|
||||||
).unwrap();
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
let file = File::open(filename)?;
|
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?) {
|
match INSTRUCTION_REGEX.captures(&line?) {
|
||||||
Some(captures) => {
|
Some(captures) => {
|
||||||
let step = get_captured_field(&captures, "step")?.parse()?;
|
let step = get_captured_field(&captures, "step")?.parse()?;
|
||||||
let dependency: String = get_captured_field(&captures, "dependency")?.parse()?;
|
let dependency: String = get_captured_field(&captures, "dependency")?.parse()?;
|
||||||
instructions.entry(dependency.clone()).or_insert(Vec::new());
|
instructions
|
||||||
let dependencies = instructions.entry(step).or_insert(Vec::new());
|
.entry(dependency.clone())
|
||||||
|
.or_insert_with(Vec::new);
|
||||||
|
let dependencies = instructions.entry(step).or_insert_with(Vec::new);
|
||||||
dependencies.push(dependency);
|
dependencies.push(dependency);
|
||||||
},
|
}
|
||||||
None => return Err(Box::new(MalformedInstruction {
|
None => {
|
||||||
details: "Malformed instruction line, no fields could be found".to_string()
|
return Err(Box::new(MalformedInstruction {
|
||||||
})),
|
details: "Malformed instruction line, no fields could be found".to_string(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Ok(instructions)
|
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>> {
|
fn get_captured_field(captures: &Captures, field: &str) -> Result<String, Box<Error>> {
|
||||||
match captures.name(field) {
|
match captures.name(field) {
|
||||||
Some(capture) => Ok(String::from(capture.as_str())),
|
Some(capture) => Ok(String::from(capture.as_str())),
|
||||||
None => return Err(Box::new(MalformedInstruction {
|
None => Err(Box::new(MalformedInstruction {
|
||||||
details: format!("Malformed instruction line, field {} could not be found", field)
|
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 {
|
loop {
|
||||||
let mut available: Vec<String> = instructions
|
let mut available: Vec<String> = instructions
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_, dependencies)| dependencies.len() == 0)
|
.filter(|(_, dependencies)| dependencies.is_empty())
|
||||||
.map(|(step, _)| step.clone())
|
.map(|(step, _)| step.clone())
|
||||||
.collect();
|
.collect();
|
||||||
if available.len() == 0 { break; }
|
if available.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
available.sort();
|
available.sort();
|
||||||
available.reverse();
|
available.reverse();
|
||||||
let next = available.pop().unwrap();
|
let next = available.pop().unwrap();
|
||||||
instructions.remove(&next);
|
instructions.remove(&next);
|
||||||
for dependencies in instructions.values_mut() {
|
for dependencies in instructions.values_mut() {
|
||||||
match dependencies.iter().position(|d| *d == next) {
|
if let Some(index) = dependencies.iter().position(|d| *d == next) {
|
||||||
Some(index) => { dependencies.remove(index); },
|
dependencies.remove(index);
|
||||||
None => ()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sequence.push(next);
|
sequence.push(next);
|
||||||
@ -110,9 +121,14 @@ mod tests {
|
|||||||
("C".to_string(), vec![]),
|
("C".to_string(), vec![]),
|
||||||
("B".to_string(), vec!["A".to_string()]),
|
("B".to_string(), vec!["A".to_string()]),
|
||||||
("D".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()]),
|
(
|
||||||
|
"E".to_string(),
|
||||||
].iter().cloned().collect();
|
vec!["B".to_string(), "D".to_string(), "F".to_string()],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
assert_eq!(read_instructions(TEST_INPUT).unwrap(), expected);
|
assert_eq!(read_instructions(TEST_INPUT).unwrap(), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,9 +140,14 @@ mod tests {
|
|||||||
("C".to_string(), vec![]),
|
("C".to_string(), vec![]),
|
||||||
("B".to_string(), vec!["A".to_string()]),
|
("B".to_string(), vec!["A".to_string()]),
|
||||||
("D".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()]),
|
(
|
||||||
|
"E".to_string(),
|
||||||
].iter().cloned().collect();
|
vec!["B".to_string(), "D".to_string(), "F".to_string()],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
assert_eq!(get_step_sequence(&mut instructions), "CABDFE");
|
assert_eq!(get_step_sequence(&mut instructions), "CABDFE");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#![warn(clippy)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user