Created tool to quickly spin up new puzzles

This commit is contained in:
Luke Else 2024-07-12 11:00:36 +01:00
parent 0ec4d01dcf
commit 1835230516
6 changed files with 52 additions and 7 deletions

45
create.py Normal file
View File

@ -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)

View File

@ -8,8 +8,8 @@ use solutions::*;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let problems: Vec<Box<dyn Solution + Sync>> = vec![
Box::new(valid_parentheses::ValidParentheses {}),
Box::new(xx::XX {}),
Box::new(valid_parentheses::ValidParentheses {})
];
let mut t = vec![];

View File

@ -9,7 +9,7 @@ pub trait Solution {
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 get_id(&self) -> u32;
fn run(&self) -> Result<Run, Box<dyn std::error::Error>> {
let start_time = SystemTime::now();
@ -29,7 +29,7 @@ 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 id: u32,
pub time: core::time::Duration,
}

View File

@ -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
}
}

View File

@ -7,10 +7,10 @@ impl Solution for XX {
&self,
_input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
Ok(Box::new("Incomplete"))
}
fn get_id(&self) -> u8 {
fn get_id(&self) -> u32 {
0
}
}

View File

@ -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<Vec<String>, Box<dyn Error>> {
pub fn get_input(id: u32, input: InputType) -> Result<Vec<String>, Box<dyn Error>> {
// Find Input File Name
let file_name = match input {
InputType::Test1 => format!("src/input/{}_test1", id),