diff --git a/create.py b/create.py new file mode 100644 index 0000000..5d65e0f --- /dev/null +++ b/create.py @@ -0,0 +1,45 @@ +puzzle_number = input("What is the puzzle ID: ") +puzzle_name = input("What is the puzzle name (Snake Case): ") +boilerplate = open(".\\src\\solutions\\xx.rs", "r").read() + +names = puzzle_name.split('_') +new_names = list() + +for name in names: + new_names.append(name.capitalize()) + +boilerplate = boilerplate.replace("xx", puzzle_name) +boilerplate = boilerplate.replace("0", puzzle_number) +boilerplate = boilerplate.replace("XX", "".join(new_names)) + +# Get 3 test inputs +for i in range(1, 4): + test_input = input("Test Input for test {}: ".format(i)) + test_result = input("What is the test result for test {}: ".format(i)) + test_file = open(".\\src\\input\\{}_test{}".format(puzzle_number, i), "w") + test_file.write(test_input) + boilerplate = boilerplate.replace("Ready", test_result, 1) + +new_code = open(".\\src\\solutions\\{}.rs".format(puzzle_name), "w") +new_code.write(boilerplate) + +# Edit main file to add the new puzzle +main_file = open(".\\src\\main.rs", "r+", encoding="utf-8") +main_file_contents = main_file.read() +old_puzzles = "Box::new(xx::XX {})," +new_puzzles = "Box::new(" + puzzle_name + "::" + "".join(new_names) + " {}),\n Box::new(xx::XX {})," +main_file_contents = main_file_contents.replace(old_puzzles, new_puzzles) +main_file.truncate(0) +main_file.seek(0) +main_file.write(main_file_contents) + + +# Edit mode file to expose the new solution +mod_file = open(".\\src\\solutions\\mod.rs", "r+", encoding="utf-8") +mod_file_contents = mod_file.read() +old_mod = "pub mod xx;" +new_mod = "pub mod " + puzzle_name + ";\npub mod xx;" +mod_file_contents = mod_file_contents.replace(old_mod, new_mod) +mod_file.truncate(0) +mod_file.seek(0) +mod_file.write(mod_file_contents) diff --git a/src/main.rs b/src/main.rs index 99d5b1c..71e74e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,8 @@ use solutions::*; #[tokio::main] async fn main() -> Result<(), Box> { let problems: Vec> = vec![ + Box::new(valid_parentheses::ValidParentheses {}), Box::new(xx::XX {}), - Box::new(valid_parentheses::ValidParentheses {}) ]; let mut t = vec![]; diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index 5aa562e..07a3d1c 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -9,7 +9,7 @@ pub trait Solution { fn solution(&self, input: &mut Vec) -> Result, Box>; // Get the id of the problem in question - fn get_id(&self) -> u8; + fn get_id(&self) -> u32; fn run(&self) -> Result> { let start_time = SystemTime::now(); @@ -29,7 +29,7 @@ pub struct Run { pub test1: Box, pub test2: Box, pub test3: Box, - pub id: u8, + pub id: u32, pub time: core::time::Duration, } diff --git a/src/solutions/valid_parentheses.rs b/src/solutions/valid_parentheses.rs index d0c5ca1..a985f00 100644 --- a/src/solutions/valid_parentheses.rs +++ b/src/solutions/valid_parentheses.rs @@ -32,7 +32,7 @@ impl Solution for ValidParentheses { Ok(Box::new(stack.len() == 0)) } - fn get_id(&self) -> u8 { + fn get_id(&self) -> u32 { 20 } } diff --git a/src/solutions/xx.rs b/src/solutions/xx.rs index aaf36fa..529b1b3 100644 --- a/src/solutions/xx.rs +++ b/src/solutions/xx.rs @@ -7,10 +7,10 @@ impl Solution for XX { &self, _input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + Ok(Box::new("Incomplete")) } - fn get_id(&self) -> u8 { + fn get_id(&self) -> u32 { 0 } } diff --git a/src/utils.rs b/src/utils.rs index 6002e9f..12224ca 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -13,7 +13,7 @@ pub enum InputType { } /// Function to get the input for a given leetcode problem -pub fn get_input(id: u8, input: InputType) -> Result, Box> { +pub fn get_input(id: u32, input: InputType) -> Result, Box> { // Find Input File Name let file_name = match input { InputType::Test1 => format!("src/input/{}_test1", id),