feat: implement benchmarks (#30)
This commit is contained in:
0
src/bin/.keep
Normal file
0
src/bin/.keep
Normal file
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* This file contains template code.
|
||||
* There is no need to edit this file unless you want to change template functionality.
|
||||
*/
|
||||
use advent_of_code::aoc_cli;
|
||||
use std::process;
|
||||
|
||||
struct Args {
|
||||
day: u8,
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, pico_args::Error> {
|
||||
let mut args = pico_args::Arguments::from_env();
|
||||
Ok(Args {
|
||||
day: args.free_from_str()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = match parse_args() {
|
||||
Ok(args) => args,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to process arguments: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
if aoc_cli::check().is_err() {
|
||||
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
match aoc_cli::download(args.day) {
|
||||
Ok(cmd_output) => {
|
||||
if !cmd_output.status.success() {
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("failed to spawn aoc-cli: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* This file contains template code.
|
||||
* There is no need to edit this file unless you want to change template functionality.
|
||||
*/
|
||||
use advent_of_code::aoc_cli;
|
||||
use std::process;
|
||||
|
||||
struct Args {
|
||||
day: u8,
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, pico_args::Error> {
|
||||
let mut args = pico_args::Arguments::from_env();
|
||||
Ok(Args {
|
||||
day: args.free_from_str()?,
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = match parse_args() {
|
||||
Ok(args) => args,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to process arguments: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
if aoc_cli::check().is_err() {
|
||||
eprintln!("command \"aoc\" not found or not callable. Try running \"cargo install aoc-cli\" to install it.");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
match aoc_cli::read(args.day) {
|
||||
Ok(cmd_output) => {
|
||||
if !cmd_output.status.success() {
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("failed to spawn aoc-cli: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* This file contains template code.
|
||||
* There is no need to edit this file unless you want to change template functionality.
|
||||
*/
|
||||
use std::{
|
||||
fs::{File, OpenOptions},
|
||||
io::Write,
|
||||
process,
|
||||
};
|
||||
|
||||
const MODULE_TEMPLATE: &str = r#"pub fn part_one(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = &advent_of_code::read_file("inputs", DAY);
|
||||
advent_of_code::solve!(DAY, 1, part_one, input);
|
||||
advent_of_code::solve!(DAY, 2, part_two, input);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let input = advent_of_code::read_file("examples", DAY);
|
||||
assert_eq!(part_one(&input), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let input = advent_of_code::read_file("examples", DAY);
|
||||
assert_eq!(part_two(&input), None);
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
fn parse_args() -> Result<u8, pico_args::Error> {
|
||||
let mut args = pico_args::Arguments::from_env();
|
||||
args.free_from_str()
|
||||
}
|
||||
|
||||
fn safe_create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
OpenOptions::new().write(true).create_new(true).open(path)
|
||||
}
|
||||
|
||||
fn create_file(path: &str) -> Result<File, std::io::Error> {
|
||||
OpenOptions::new().write(true).create(true).open(path)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let day = match parse_args() {
|
||||
Ok(day) => day,
|
||||
Err(_) => {
|
||||
eprintln!("Need to specify a day (as integer). example: `cargo scaffold 7`");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let day_padded = format!("{day:02}");
|
||||
|
||||
let input_path = format!("src/inputs/{day_padded}.txt");
|
||||
let example_path = format!("src/examples/{day_padded}.txt");
|
||||
let module_path = format!("src/bin/{day_padded}.rs");
|
||||
|
||||
let mut file = match safe_create_file(&module_path) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create module file: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
match file.write_all(MODULE_TEMPLATE.replace("DAY", &day.to_string()).as_bytes()) {
|
||||
Ok(_) => {
|
||||
println!("Created module file \"{}\"", &module_path);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to write module contents: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
match create_file(&input_path) {
|
||||
Ok(_) => {
|
||||
println!("Created empty input file \"{}\"", &input_path);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create input file: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
match create_file(&example_path) {
|
||||
Ok(_) => {
|
||||
println!("Created empty example file \"{}\"", &example_path);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create example file: {e}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
println!("---");
|
||||
println!(
|
||||
"🎄 Type `cargo solve {}` to run your solution.",
|
||||
&day_padded
|
||||
);
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* This file contains template code.
|
||||
* There is no need to edit this file unless you want to change template functionality.
|
||||
*/
|
||||
|
||||
use std::process::{self, Command, Stdio};
|
||||
|
||||
struct Args {
|
||||
day: u8,
|
||||
release: bool,
|
||||
submit: Option<u8>,
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, pico_args::Error> {
|
||||
let mut args = pico_args::Arguments::from_env();
|
||||
Ok(Args {
|
||||
day: args.free_from_str()?,
|
||||
release: args.contains("--release"),
|
||||
submit: args.opt_value_from_str("--submit")?,
|
||||
})
|
||||
}
|
||||
|
||||
fn run_solution(day: u8, release: bool, submit_part: Option<u8>) -> Result<(), std::io::Error> {
|
||||
let day_padded = format!("{:02}", day);
|
||||
|
||||
let mut cmd_args = vec!["run".to_string(), "--bin".to_string(), day_padded];
|
||||
|
||||
if release {
|
||||
cmd_args.push("--release".to_string());
|
||||
}
|
||||
|
||||
if let Some(submit_part) = submit_part {
|
||||
cmd_args.push("--".to_string());
|
||||
cmd_args.push("--submit".to_string());
|
||||
cmd_args.push(submit_part.to_string())
|
||||
}
|
||||
|
||||
let mut cmd = Command::new("cargo")
|
||||
.args(&cmd_args)
|
||||
.stdout(Stdio::inherit())
|
||||
.stderr(Stdio::inherit())
|
||||
.spawn()?;
|
||||
|
||||
cmd.wait()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = match parse_args() {
|
||||
Ok(args) => args,
|
||||
Err(e) => {
|
||||
eprintln!("Failed to process arguments: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
run_solution(args.day, args.release, args.submit).unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user