feat: return option from solution parts

this allows to print _not solved_ for solution parts that haven't been solved yet.

adjust `solve` macro to handle one solution part rather than both.
this allows for easier debugging of real input.
This commit is contained in:
Felix Spöttel 2022-10-29 17:04:13 +02:00
parent 9eb5e488e2
commit ec5859083a
3 changed files with 25 additions and 21 deletions

View File

@ -4,16 +4,18 @@ use std::{
process,
};
const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> u32 {
0
const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> Option<u32> {
None
}
pub fn part_two(input: &str) -> u32 {
0
pub fn part_two(input: &str) -> Option<u32> {
None
}
fn main() {
aoc::solve!(&aoc::read_file("inputs", DAY), part_one, part_two)
let input = &aoc::read_file("inputs", DAY);
aoc::solve!(1, part_one, input);
aoc::solve!(2, part_two, input);
}
#[cfg(test)]
@ -24,14 +26,14 @@ mod tests {
fn test_part_one() {
use aoc::read_file;
let input = read_file("examples", DAY);
assert_eq!(part_one(&input), 0);
assert_eq!(part_one(&input), None);
}
#[test]
fn test_part_two() {
use aoc::read_file;
let input = read_file("examples", DAY);
assert_eq!(part_two(&input), 0);
assert_eq!(part_two(&input), None);
}
}
"###;

View File

@ -11,28 +11,30 @@ pub const ANSI_RESET: &str = "\x1b[0m";
#[macro_export]
macro_rules! solve {
($input:expr, $part_one:ident, $part_two:ident) => {{
($part:expr, $solver:ident, $input:expr) => {{
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
use std::fmt::Display;
use std::time::Instant;
fn print_result<T: Display>(func: impl FnOnce(&str) -> T, input: &str) {
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();
println!(
"{} {}(elapsed: {:.2?}){}",
result, ANSI_ITALIC, elapsed, ANSI_RESET
);
match result {
Some(result) => {
println!(
"{} {}(elapsed: {:.2?}){}",
result, ANSI_ITALIC, elapsed, ANSI_RESET
);
},
None => {
println!("not solved.")
}
}
}
println!("🎄 {}Part 1{} 🎄", ANSI_BOLD, ANSI_RESET);
println!("");
print_result($part_one, $input);
println!("");
println!("🎄 {}Part 2{} 🎄", ANSI_BOLD, ANSI_RESET);
println!("");
print_result($part_two, $input);
println!("🎄 {}Part {}{} 🎄", ANSI_BOLD, $part, ANSI_RESET);
print_result($solver, $input);
}};
}

View File

@ -18,7 +18,7 @@ fn main() {
let output = String::from_utf8(cmd.stdout).unwrap();
let is_empty = output.is_empty();
println!("{}", if is_empty { "Not solved." } else { &output });
println!("{}", if is_empty { "Not solved." } else { &output.trim() });
if is_empty {
0_f64