#1 Created base repo ready to start solving some problems.

This commit is contained in:
Luke Else 2024-07-05 11:14:11 +01:00
parent bbca87646a
commit 2318361bfc
8 changed files with 206 additions and 0 deletions

7
Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "OxidisedLeetCode"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1.38.0", features = ["full"] }

0
src/input/0_test1 Normal file
View File

0
src/input/0_test2 Normal file
View File

0
src/input/0_test3 Normal file
View File

40
src/main.rs Normal file
View File

@ -0,0 +1,40 @@
mod solutions;
mod utils;
use std::{error::Error, time::SystemTime};
use solutions::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let problems: Vec<Box<dyn Solution + Sync>> = 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(())
}

35
src/solutions/mod.rs Normal file
View File

@ -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<String>) -> Result<Box<dyn Display + Sync>, Box<dyn Error>>;
// Get the id of the problem in question
fn get_id(&self) -> u8;
fn run(&self) -> Result<Run, Box<dyn std::error::Error>> {
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<dyn Display + Sync>,
pub test2: Box<dyn Display + Sync>,
pub test3: Box<dyn Display + Sync>,
pub id: u8,
pub time: core::time::Duration,
}
unsafe impl Send for Run {}

59
src/solutions/xx.rs Normal file
View File

@ -0,0 +1,59 @@
use super::Solution;
pub struct XX {}
impl Solution for XX {
fn solution(
&self,
_input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
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");
}
}

65
src/utils.rs Normal file
View File

@ -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<Vec<String>, Box<dyn Error>> {
// 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;
}
}