From eacf990729ba9210933cf0664b544bb0db2dbaac Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Fri, 1 Nov 2024 16:52:33 +0100 Subject: [PATCH] Script to prepare days and years --- Cargo.lock | 4 +++ Cargo.toml | 5 ++++ src/main.rs | 61 +++++++++++++++++++++++++++++++++++++++++ template/bin/d.rs.tmpl | 20 ++++++++++++++ template/days/d.rs.tmpl | 26 ++++++++++++++++++ y2016/src/lib.rs | 1 + y2017/src/lib.rs | 1 + y2018/src/lib.rs | 1 + y2019/src/lib.rs | 1 + y2020/src/lib.rs | 1 + y2021/src/lib.rs | 1 + y2023/src/lib.rs | 1 + y2024/src/lib.rs | 1 + 13 files changed, 124 insertions(+) create mode 100644 src/main.rs create mode 100644 template/bin/d.rs.tmpl create mode 100644 template/days/d.rs.tmpl diff --git a/Cargo.lock b/Cargo.lock index ca953c6..62ef042 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 1eb683a..1bf1cb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,8 @@ +[package] +name = "genaoc" +version = "0.1.0" +edition = "2021" + [workspace] resolver = "2" members = [ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b17644a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,61 @@ +use std::{env, fs, path::Path, process::Command}; + +fn main() -> Result<(), std::io::Error> { + let args: Vec = 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(()) +} diff --git a/template/bin/d.rs.tmpl b/template/bin/d.rs.tmpl new file mode 100644 index 0000000..b907d64 --- /dev/null +++ b/template/bin/d.rs.tmpl @@ -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)); +} diff --git a/template/days/d.rs.tmpl b/template/days/d.rs.tmpl new file mode 100644 index 0000000..9025dcc --- /dev/null +++ b/template/days/d.rs.tmpl @@ -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); + } +} diff --git a/y2016/src/lib.rs b/y2016/src/lib.rs index e69de29..8b13789 100644 --- a/y2016/src/lib.rs +++ b/y2016/src/lib.rs @@ -0,0 +1 @@ + diff --git a/y2017/src/lib.rs b/y2017/src/lib.rs index e69de29..8b13789 100644 --- a/y2017/src/lib.rs +++ b/y2017/src/lib.rs @@ -0,0 +1 @@ + diff --git a/y2018/src/lib.rs b/y2018/src/lib.rs index e69de29..8b13789 100644 --- a/y2018/src/lib.rs +++ b/y2018/src/lib.rs @@ -0,0 +1 @@ + diff --git a/y2019/src/lib.rs b/y2019/src/lib.rs index e69de29..8b13789 100644 --- a/y2019/src/lib.rs +++ b/y2019/src/lib.rs @@ -0,0 +1 @@ + diff --git a/y2020/src/lib.rs b/y2020/src/lib.rs index e69de29..8b13789 100644 --- a/y2020/src/lib.rs +++ b/y2020/src/lib.rs @@ -0,0 +1 @@ + diff --git a/y2021/src/lib.rs b/y2021/src/lib.rs index e69de29..8b13789 100644 --- a/y2021/src/lib.rs +++ b/y2021/src/lib.rs @@ -0,0 +1 @@ + diff --git a/y2023/src/lib.rs b/y2023/src/lib.rs index e69de29..8b13789 100644 --- a/y2023/src/lib.rs +++ b/y2023/src/lib.rs @@ -0,0 +1 @@ + diff --git a/y2024/src/lib.rs b/y2024/src/lib.rs index e69de29..8b13789 100644 --- a/y2024/src/lib.rs +++ b/y2024/src/lib.rs @@ -0,0 +1 @@ +