Script to prepare days and years

This commit is contained in:
Fabian Schmidt 2024-11-01 16:52:33 +01:00
parent 828566cce3
commit a687f18918
13 changed files with 124 additions and 0 deletions

4
Cargo.lock generated
View File

@ -2,6 +2,10 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "genaoc"
version = "0.1.0"
[[package]]
name = "md5"
version = "0.7.0"

View File

@ -1,3 +1,8 @@
[package]
name = "genaoc"
version = "0.1.0"
edition = "2021"
[workspace]
resolver = "2"
members = [

61
src/main.rs Normal file
View File

@ -0,0 +1,61 @@
use std::{env, fs, path::Path, process::Command};
fn main() -> Result<(), std::io::Error> {
let args: Vec<String> = env::args().collect();
if args.len() == 1 {
println!("Too few arguments");
println!("Please enter a year, or a year and day");
return Err(std::io::ErrorKind::InvalidInput.into());
} else if args.len() > 3 {
println!("Too many arguments");
println!("Please enter a year, or a year and day");
return Err(std::io::ErrorKind::InvalidInput.into());
} else if args.len() == 2 {
prepare_year(&args[1])?;
} else if args.len() == 3 {
prepare_day(&args[1], &args[2])?;
}
match Command::new("cargo").arg("fmt").status() {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}
fn prepare_year(year: &str) -> Result<(), std::io::Error> {
let root = env!("CARGO_MANIFEST_DIR");
if Path::new(&format!("{root}/y{year}")).exists() {
panic!("Folder y{year} already exists")
}
// create workspace
match Command::new("cargo")
.arg("new")
.arg(format!("y{year}"))
.status()
{
Ok(_status) => Ok(()),
Err(e) => Err(e),
}?;
// remove main.rs
fs::remove_file(format!("{root}/y{year}/src/main.rs"))?;
fs::create_dir(format!("{root}/y{year}/src/bin"))?;
fs::create_dir(format!("{root}/y{year}/src/days"))?;
fs::write(format!("{root}/y{year}/src/lib.rs"), "pub mod days;")?;
fs::write(format!("{root}/y{year}/src/days/mod.rs"), "")?;
prepare_day(year, "1")
}
fn prepare_day(year: &str, day: &str) -> Result<(), std::io::Error> {
let root = env!("CARGO_MANIFEST_DIR");
let bin = fs::read_to_string(format!("{root}/template/bin/d.rs.tmpl"))?
.replace("{{YEAR}}", year)
.replace("{{DAY}}", day);
let dayfile = fs::read_to_string(format!("{root}/template/days/d.rs.tmpl"))?;
let mut modfile = fs::read_to_string(format!("{root}/y{year}/src/days/mod.rs"))?;
modfile.push_str(format!("\npub mod d{day};").as_str());
let bin_path = format!("{root}/y{year}/src/bin/d{day}.rs");
fs::write(bin_path, bin)?;
fs::write(format!("{root}/y{year}/src/days/d{day}.rs"), dayfile)?;
fs::write(format!("{root}/y{year}/src/days/mod.rs"), modfile)?;
Ok(())
}

20
template/bin/d.rs.tmpl Normal file
View File

@ -0,0 +1,20 @@
use std::fs;
use y{{YEAR}}::days::d{{DAY}};
fn main() {
part1();
part2();
}
fn part1() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/{{DAY}}_input.txt")).unwrap();
println!("{}", d{{DAY}}::process_part1(&content));
}
fn part2() {
let root = env!("CARGO_MANIFEST_DIR");
let content = fs::read_to_string(format!("{root}/resources/{{DAY}}_input.txt")).unwrap();
println!("{}", d{{DAY}}::process_part2(&content));
}

26
template/days/d.rs.tmpl Normal file
View File

@ -0,0 +1,26 @@
pub fn process_part1(input: &str) -> i32 {
0
}
pub fn process_part2(input: &str) -> i32 {
0
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &str = "{{EXAMPLE}}";
#[test]
fn part1() {
let result = process_part1(INPUT);
assert_eq!(result, 0);
}
#[test]
fn part2() {
let result = process_part2(INPUT);
assert_eq!(result, 0);
}
}

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@

View File

@ -0,0 +1 @@