feat: trim solution macro

This commit is contained in:
Matej Janezic 2023-11-27 21:34:27 +01:00
parent 000004209d
commit 0000043020
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
3 changed files with 33 additions and 30 deletions

6
Cargo.lock generated
View File

@ -19,7 +19,7 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]] [[package]]
name = "aoc" name = "aoc"
version = "42.0.0" version = "43.0.0"
dependencies = [ dependencies = [
"itertools", "itertools",
] ]
@ -105,7 +105,7 @@ checksum = "03d8c417d7a8cb362e0c37e5d815f5eb7c37f79ff93707329d5a194e42e54ca0"
[[package]] [[package]]
name = "download" name = "download"
version = "42.0.0" version = "43.0.0"
dependencies = [ dependencies = [
"dotenvy", "dotenvy",
"pico-args", "pico-args",
@ -643,7 +643,7 @@ checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]] [[package]]
name = "scaffold" name = "scaffold"
version = "42.0.0" version = "43.0.0"
dependencies = [ dependencies = [
"dotenvy", "dotenvy",
"pico-args", "pico-args",

View File

@ -19,7 +19,7 @@ members = ["utils/download", "utils/scaffold"]
[workspace.package] [workspace.package]
description = "template for advent of code" description = "template for advent of code"
readme = "README.md" readme = "README.md"
version = "42.0.0" version = "43.0.0"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = ["Matej Janežič <janezic.mj@gmail.com>"] authors = ["Matej Janežič <janezic.mj@gmail.com>"]

View File

@ -10,36 +10,39 @@ pub const ANSI_ITALIC: &str = "\x1b[3m";
pub const ANSI_BOLD: &str = "\x1b[1m"; pub const ANSI_BOLD: &str = "\x1b[1m";
pub const ANSI_RESET: &str = "\x1b[0m"; pub const ANSI_RESET: &str = "\x1b[0m";
use std::fmt::Display;
use std::time::Duration;
use std::time::Instant;
fn time_solution<T>(func: impl FnOnce(&str) -> Option<T>, input: &str) -> Option<(T, Duration)> {
let timer = Instant::now();
let result = func(input);
let elapsed = timer.elapsed();
result.map(|result| (result, elapsed))
}
pub fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str, part: u8) {
match time_solution(func, input) {
Some((result, elapsed)) => {
println!(
"{}Part {}{}: {} {}(elapsed: {:.2?}){}",
ANSI_BOLD, part, ANSI_RESET, result, ANSI_ITALIC, elapsed, ANSI_RESET
);
}
None => {
println!("{}Part {}{}: not solved.", ANSI_BOLD, part, ANSI_RESET)
}
}
}
#[macro_export] #[macro_export]
macro_rules! solution { macro_rules! solution {
($day:expr) => { ($day:expr) => {
use aoc::template::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
use std::fmt::Display;
use std::time::Instant;
fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str) {
let timer = Instant::now();
let result = func(input);
let elapsed = timer.elapsed();
match result {
Some(result) => {
println!(
"{} {}(elapsed: {:.2?}){}",
result, ANSI_ITALIC, elapsed, ANSI_RESET
);
}
None => {
println!("not solved.")
}
}
}
fn main() { fn main() {
let input = aoc::template::read_file("inputs", 1); let input = aoc::template::read_file("inputs", $day);
print!("{}Part {}{}: ", ANSI_BOLD, 1, ANSI_RESET); aoc::template::print_result(part_one, &input, 1);
print_result(part_one, &input); aoc::template::print_result(part_two, &input, 2);
print!("{}Part {}{}: ", ANSI_BOLD, 2, ANSI_RESET);
print_result(part_two, &input);
} }
}; };
} }