From 2318361bfcf131da98a6767cf59487fafd15b699 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Fri, 5 Jul 2024 11:14:11 +0100 Subject: [PATCH] #1 Created base repo ready to start solving some problems. --- Cargo.toml | 7 +++++ src/input/0_test1 | 0 src/input/0_test2 | 0 src/input/0_test3 | 0 src/main.rs | 40 +++++++++++++++++++++++++++ src/solutions/mod.rs | 35 ++++++++++++++++++++++++ src/solutions/xx.rs | 59 ++++++++++++++++++++++++++++++++++++++++ src/utils.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 206 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/input/0_test1 create mode 100644 src/input/0_test2 create mode 100644 src/input/0_test3 create mode 100644 src/main.rs create mode 100644 src/solutions/mod.rs create mode 100644 src/solutions/xx.rs create mode 100644 src/utils.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..677cd49 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "OxidisedLeetCode" +version = "0.1.0" +edition = "2021" + +[dependencies] +tokio = { version = "1.38.0", features = ["full"] } diff --git a/src/input/0_test1 b/src/input/0_test1 new file mode 100644 index 0000000..e69de29 diff --git a/src/input/0_test2 b/src/input/0_test2 new file mode 100644 index 0000000..e69de29 diff --git a/src/input/0_test3 b/src/input/0_test3 new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4752654 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,40 @@ +mod solutions; +mod utils; + +use std::{error::Error, time::SystemTime}; + +use solutions::*; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let problems: Vec> = vec![Box::new(xx::XX {})]; + + let mut t = vec![]; + + // Run through and generate solutions + for problem in problems.leak() { + let task = tokio::spawn(async { problem.run().unwrap() }); + t.push(task); + } + + let start_time = SystemTime::now(); + + let mut problems = vec![]; + for thread in t { + problems.push(thread.await?); + } + + for problem in problems { + println!("========= Problem {} ==========", problem.id); + println!("Test1 Result: {}", problem.test1); + println!("Test2 Result: {}", problem.test2); + println!("Test3 Result: {}", problem.test3); + println!("========= {}ms =========\n", problem.time.as_millis()); + } + + println!( + "Ran LeetCode in {}ms", + SystemTime::now().duration_since(start_time)?.as_millis() + ); + Ok(()) +} diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs new file mode 100644 index 0000000..5cec3eb --- /dev/null +++ b/src/solutions/mod.rs @@ -0,0 +1,35 @@ +use crate::utils::{self, get_input}; +use std::{error::Error, fmt::Display, time::SystemTime}; + +pub mod xx; + +pub trait Solution { + // Run and return the solution to the problem + fn solution(&self, input: &mut Vec) -> Result, Box>; + + // Get the id of the problem in question + fn get_id(&self) -> u8; + + fn run(&self) -> Result> { + let start_time = SystemTime::now(); + + let run = Run { + test1: self.solution(get_input(self.get_id(), utils::InputType::Test1)?.as_mut())?, + test2: self.solution(get_input(self.get_id(), utils::InputType::Test2)?.as_mut())?, + test3: self.solution(get_input(self.get_id(), utils::InputType::Test3)?.as_mut())?, + id: self.get_id(), + time: SystemTime::now().duration_since(start_time)?, + }; + Ok(run) + } +} + +pub struct Run { + pub test1: Box, + pub test2: Box, + pub test3: Box, + pub id: u8, + pub time: core::time::Duration, +} + +unsafe impl Send for Run {} diff --git a/src/solutions/xx.rs b/src/solutions/xx.rs new file mode 100644 index 0000000..b64a4e8 --- /dev/null +++ b/src/solutions/xx.rs @@ -0,0 +1,59 @@ +use super::Solution; + +pub struct XX {} + +impl Solution for XX { + fn solution( + &self, + _input: &mut Vec, + ) -> Result, Box> { + Ok(Box::new("Ready")) + } + + fn get_id(&self) -> u8 { + 0 + } +} + +impl XX {} + +/// Test from puzzle input +#[cfg(test)] +mod test { + use super::*; + use crate::*; + + #[test] + fn test1() { + let solution = xx::XX {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test1) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "Ready"); + } + + #[test] + fn test2() { + let solution = xx::XX {}; + + //Complete the Challenge + let answer = solution + .solution( + utils::get_input(solution.get_id(), utils::InputType::Test2) + .unwrap() + .as_mut(), + ) + .unwrap() + .to_string(); + + assert_eq!(answer, "Ready"); + } +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..6002e9f --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,65 @@ +use std::{ + error::Error, + fs::File, + io::{prelude::*, BufReader}, +}; + +/// Enum used to specify input +#[allow(unused)] +pub enum InputType { + Test1, + Test2, + Test3, +} + +/// Function to get the input for a given leetcode problem +pub fn get_input(id: u8, input: InputType) -> Result, Box> { + // Find Input File Name + let file_name = match input { + InputType::Test1 => format!("src/input/{}_test1", id), + InputType::Test2 => format!("src/input/{}_test2", id), + InputType::Test3 => format!("src/input/{}_test3", id), + }; + + // Read file into buffer + let file = File::open(file_name)?; + let reader = BufReader::new(file); + + // Convert itterate buffer and return string vec + let data = reader + .lines() + .map(|s| s.unwrap_or(String::from(""))) + .collect(); + + Ok(data) +} + +// +// Below this line are a series of utils that can be re-used +// for any of the problems identified +#[allow(unused)] +// +///========================================================= +// + +pub fn lcm(first: u64, second: u64) -> u64 { + first * second / gcd(first, second) +} + +pub fn gcd(first: u64, second: u64) -> u64 { + let mut max = first; + let mut min = second; + if min > max { + std::mem::swap(&mut max, &mut min); + } + + loop { + let res = max % min; + if res == 0 { + return min; + } + + max = min; + min = res; + } +}