feat: add simple cli to solve macro
This commit is contained in:
@@ -78,8 +78,7 @@ pub fn part_two(input: &str) -> Option<isize> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = &aoc::read_file("inputs", 1);
|
let input = &aoc::read_file("inputs", 1);
|
||||||
aoc::solve!(1, part_one, input);
|
aoc::solve!(part_one, part_two, input);
|
||||||
aoc::solve!(2, part_two, input);
|
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -60,8 +60,7 @@ pub fn part_two(input: &str) -> Option<String> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = &aoc::read_file("inputs", 2);
|
let input = &aoc::read_file("inputs", 2);
|
||||||
aoc::solve!(1, part_one, input);
|
aoc::solve!(part_one, part_two, input);
|
||||||
aoc::solve!(2, part_two, input);
|
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -43,8 +43,7 @@ pub fn part_two(input: &str) -> Option<usize> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = &aoc::read_file("inputs", 3);
|
let input = &aoc::read_file("inputs", 3);
|
||||||
aoc::solve!(1, part_one, input);
|
aoc::solve!(part_one, part_two, input);
|
||||||
aoc::solve!(2, part_two, input);
|
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -79,8 +79,7 @@ pub fn part_two(input: &str) -> Option<usize> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = &aoc::read_file("inputs", 4);
|
let input = &aoc::read_file("inputs", 4);
|
||||||
aoc::solve!(1, part_one, input);
|
aoc::solve!(part_one, part_one, input);
|
||||||
aoc::solve!(2, part_two, input);
|
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -59,8 +59,7 @@ pub fn part_two(input: &str) -> Option<String> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = &aoc::read_file("inputs", 5);
|
let input = &aoc::read_file("inputs", 5);
|
||||||
aoc::solve!(1, part_one, input);
|
aoc::solve!(part_one, part_two, input);
|
||||||
aoc::solve!(2, part_two, input);
|
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -30,8 +30,7 @@ pub fn part_two(input: &str) -> Option<String> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = &aoc::read_file("inputs", 6);
|
let input = &aoc::read_file("inputs", 6);
|
||||||
aoc::solve!(1, part_one, input);
|
aoc::solve!(part_one, part_two, input);
|
||||||
aoc::solve!(2, part_two, input);
|
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
@@ -64,8 +64,7 @@ pub fn part_two(input: &str) -> Option<u32> {
|
|||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = &aoc::read_file("inputs", 7);
|
let input = &aoc::read_file("inputs", 7);
|
||||||
aoc::solve!(1, part_one, input);
|
aoc::solve!(part_one,part_two, input);
|
||||||
aoc::solve!(2, part_two, input);
|
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
46
src/lib.rs
46
src/lib.rs
@@ -12,12 +12,28 @@ pub const ANSI_RESET: &str = "\x1b[0m";
|
|||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! solve {
|
macro_rules! solve {
|
||||||
($part:expr, $solver:ident, $input:expr) => {{
|
($solver_one:ident, $solver_two:ident, $input:expr) => {
|
||||||
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
|
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str) {
|
fn print_result<T: Display>(
|
||||||
|
func: impl FnOnce(&str) -> Option<T>,
|
||||||
|
part: u8,
|
||||||
|
input: &str,
|
||||||
|
quiet: bool,
|
||||||
|
) {
|
||||||
|
if quiet {
|
||||||
|
let result = func(input);
|
||||||
|
if let Some(r) = result {
|
||||||
|
println!("{}", r);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("🎄 {}Part {}{} 🎄", ANSI_BOLD, part, ANSI_RESET);
|
||||||
|
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
let result = func(input);
|
let result = func(input);
|
||||||
let elapsed = timer.elapsed();
|
let elapsed = timer.elapsed();
|
||||||
@@ -34,9 +50,29 @@ macro_rules! solve {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🎄 {}Part {}{} 🎄", ANSI_BOLD, $part, ANSI_RESET);
|
let mut args = pico_args::Arguments::from_env();
|
||||||
print_result($solver, $input);
|
|
||||||
}};
|
let part = args
|
||||||
|
.opt_value_from_str("-p")
|
||||||
|
.expect("There was an error parsing cli args.");
|
||||||
|
let quiet = args.contains("-q");
|
||||||
|
|
||||||
|
match part {
|
||||||
|
None => {
|
||||||
|
print_result($solver_one, 1, $input, quiet);
|
||||||
|
print_result($solver_two, 2, $input, quiet);
|
||||||
|
}
|
||||||
|
Some(pp) => match pp {
|
||||||
|
1 => {
|
||||||
|
print_result($solver_one, 1, $input, quiet);
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
print_result($solver_two, 2, $input, quiet);
|
||||||
|
}
|
||||||
|
_ => eprintln!("error: expected -p to be 1 or 2"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_file(folder: &str, day: u8) -> String {
|
pub fn read_file(folder: &str, day: u8) -> String {
|
||||||
|
|||||||
Reference in New Issue
Block a user