Cleaned up runner

This commit is contained in:
Luke Else 2023-12-02 13:42:55 +00:00
parent cb4b54e130
commit 554319e068
3 changed files with 65 additions and 46 deletions

View File

@ -1,40 +1,20 @@
mod solutions; mod solutions;
mod utils; mod utils;
use std::{error::Error, time::SystemTime}; use std::error::Error;
use solutions::*; use solutions::*;
use crate::utils::get_input;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let days: Vec<Box<dyn Solution>> = vec![Box::new(day01::Day01 {}), Box::new(day02::Day02 {})]; let days: Vec<Box<dyn Solution>> = vec![
Box::new(day01::Day01 {}),
Box::new(day02::Day02 {}),
Box::new(day03::Day03 {}),
];
// Run through and generate solutions // Run through and generate solutions
for day in days.iter().rev() { for day in days.iter().rev() {
println!("========== Day {} ==========", day.get_day()); day.run()?;
let start_time = SystemTime::now();
println!(
"Part1 Test: {}",
day.part1(get_input(day.get_day(), utils::InputType::Test1)?.as_mut())?
);
println!(
"Part1 Result: {}",
day.part1(get_input(day.get_day(), utils::InputType::Actual)?.as_mut())?
);
println!(
"Part2 Test: {}",
day.part2(get_input(day.get_day(), utils::InputType::Test2)?.as_mut())?
);
println!(
"Part2 Result: {}",
day.part2(get_input(day.get_day(), utils::InputType::Actual)?.as_mut())?
);
println!(
"========= {}ms =========",
SystemTime::now().duration_since(start_time)?.as_millis()
);
} }
Ok(()) Ok(())

View File

@ -1,6 +1,6 @@
use super::Solution; use super::Solution;
use std::str::FromStr;
use fancy_regex::Regex; use fancy_regex::Regex;
use std::str::FromStr;
use strum_macros::EnumString; use strum_macros::EnumString;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
@ -15,7 +15,7 @@ enum WrittenNumbers {
six = 6, six = 6,
seven = 7, seven = 7,
eight = 8, eight = 8,
nine = 9 nine = 9,
} }
pub struct Day01 {} pub struct Day01 {}
@ -53,7 +53,11 @@ impl Solution for Day01 {
} }
impl Day01 { impl Day01 {
fn regex_get_num(&self, regex: &str, input: &mut String) -> Result<u32, Box<dyn std::error::Error>> { fn regex_get_num(
&self,
regex: &str,
input: &mut String,
) -> Result<u32, Box<dyn std::error::Error>> {
let re = Regex::new(regex).unwrap(); let re = Regex::new(regex).unwrap();
// Really don't like this :( // Really don't like this :(
@ -66,17 +70,14 @@ impl Day01 {
let input = input.replace("eightwo", "eighttwo"); let input = input.replace("eightwo", "eighttwo");
// Get all single digits out of string // Get all single digits out of string
let matches: Vec<&str> = re let matches: Vec<&str> = re.find_iter(&input).map(|m| m.unwrap().as_str()).collect();
.find_iter(&input)
.map(|m| m.unwrap().as_str())
.collect();
// Convert everything to a number // Convert everything to a number
let mut nums: Vec<u32> = vec![]; let mut nums: Vec<u32> = vec![];
for m in matches.clone() { for m in matches.clone() {
match Day01::convert_written_number(m) { match Day01::convert_written_number(m) {
Some(n) => nums.push(n), Some(n) => nums.push(n),
None => nums.push(m.parse::<u32>()?) None => nums.push(m.parse::<u32>()?),
} }
} }
@ -98,29 +99,39 @@ impl Day01 {
/// Test from puzzle input /// Test from puzzle input
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::*;
use super::*; use super::*;
use crate::*;
#[test] #[test]
fn part1() { fn part1() {
let challenge = day01::Day01{}; let challenge = day01::Day01 {};
//Complete the Challenge //Complete the Challenge
let answer = challenge.part1( let answer = challenge
get_input(challenge.get_day(), utils::InputType::Test1).unwrap().as_mut() .part1(
).unwrap().to_string(); get_input(challenge.get_day(), utils::InputType::Test1)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "142"); assert_eq!(answer, "142");
} }
#[test] #[test]
fn part2() { fn part2() {
let challenge = day01::Day01{}; let challenge = day01::Day01 {};
//Complete the Challenge //Complete the Challenge
let answer = challenge.part2( let answer = challenge
get_input(challenge.get_day(), utils::InputType::Test2).unwrap().as_mut() .part2(
).unwrap().to_string(); get_input(challenge.get_day(), utils::InputType::Test2)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "281"); assert_eq!(answer, "281");
} }

View File

@ -2,10 +2,38 @@ pub mod day01;
pub mod day02; pub mod day02;
pub mod day03; pub mod day03;
use std::{error::Error, fmt::Display}; use crate::utils::{self, get_input};
use std::{error::Error, fmt::Display, time::SystemTime};
pub trait Solution { pub trait Solution {
fn part1(&self, input: &mut Vec<String>) -> Result<Box<dyn Display>, Box<dyn Error>>; fn part1(&self, input: &mut Vec<String>) -> Result<Box<dyn Display>, Box<dyn Error>>;
fn part2(&self, input: &mut Vec<String>) -> Result<Box<dyn Display>, Box<dyn Error>>; fn part2(&self, input: &mut Vec<String>) -> Result<Box<dyn Display>, Box<dyn Error>>;
fn get_day(&self) -> u8; fn get_day(&self) -> u8;
fn run(&self) -> Result<(), Box<dyn std::error::Error>> {
println!("========== Day {} ==========", self.get_day());
let start_time = SystemTime::now();
println!(
"Part1 Test: {}",
self.part1(get_input(self.get_day(), utils::InputType::Test1)?.as_mut())?
);
println!(
"Part1 Result: {}",
self.part1(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?
);
println!(
"Part2 Test: {}",
self.part2(get_input(self.get_day(), utils::InputType::Test2)?.as_mut())?
);
println!(
"Part2 Result: {}",
self.part2(get_input(self.get_day(), utils::InputType::Actual)?.as_mut())?
);
println!(
"========= {}ms =========",
SystemTime::now().duration_since(start_time)?.as_millis()
);
Ok(())
}
} }