Skip to content

Commit ec58590

Browse files
committed
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.
1 parent 9eb5e48 commit ec58590

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

src/bin/scaffold.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ use std::{
44
process,
55
};
66

7-
const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> u32 {
8-
0
7+
const MODULE_TEMPLATE: &str = r###"pub fn part_one(input: &str) -> Option<u32> {
8+
None
99
}
1010
11-
pub fn part_two(input: &str) -> u32 {
12-
0
11+
pub fn part_two(input: &str) -> Option<u32> {
12+
None
1313
}
1414
1515
fn main() {
16-
aoc::solve!(&aoc::read_file("inputs", DAY), part_one, part_two)
16+
let input = &aoc::read_file("inputs", DAY);
17+
aoc::solve!(1, part_one, input);
18+
aoc::solve!(2, part_two, input);
1719
}
1820
1921
#[cfg(test)]
@@ -24,14 +26,14 @@ mod tests {
2426
fn test_part_one() {
2527
use aoc::read_file;
2628
let input = read_file("examples", DAY);
27-
assert_eq!(part_one(&input), 0);
29+
assert_eq!(part_one(&input), None);
2830
}
2931
3032
#[test]
3133
fn test_part_two() {
3234
use aoc::read_file;
3335
let input = read_file("examples", DAY);
34-
assert_eq!(part_two(&input), 0);
36+
assert_eq!(part_two(&input), None);
3537
}
3638
}
3739
"###;

src/lib.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,30 @@ pub const ANSI_RESET: &str = "\x1b[0m";
1111

1212
#[macro_export]
1313
macro_rules! solve {
14-
($input:expr, $part_one:ident, $part_two:ident) => {{
14+
($part:expr, $solver:ident, $input:expr) => {{
1515
use aoc::{ANSI_BOLD, ANSI_ITALIC, ANSI_RESET};
1616
use std::fmt::Display;
1717
use std::time::Instant;
1818

19-
fn print_result<T: Display>(func: impl FnOnce(&str) -> T, input: &str) {
19+
fn print_result<T: Display>(func: impl FnOnce(&str) -> Option<T>, input: &str) {
2020
let timer = Instant::now();
2121
let result = func(input);
2222
let elapsed = timer.elapsed();
23-
println!(
24-
"{} {}(elapsed: {:.2?}){}",
25-
result, ANSI_ITALIC, elapsed, ANSI_RESET
26-
);
23+
match result {
24+
Some(result) => {
25+
println!(
26+
"{} {}(elapsed: {:.2?}){}",
27+
result, ANSI_ITALIC, elapsed, ANSI_RESET
28+
);
29+
},
30+
None => {
31+
println!("not solved.")
32+
}
33+
}
2734
}
2835

29-
println!("🎄 {}Part 1{} 🎄", ANSI_BOLD, ANSI_RESET);
30-
println!("");
31-
print_result($part_one, $input);
32-
println!("");
33-
println!("🎄 {}Part 2{} 🎄", ANSI_BOLD, ANSI_RESET);
34-
println!("");
35-
print_result($part_two, $input);
36+
println!("🎄 {}Part {}{} 🎄", ANSI_BOLD, $part, ANSI_RESET);
37+
print_result($solver, $input);
3638
}};
3739
}
3840

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() {
1818
let output = String::from_utf8(cmd.stdout).unwrap();
1919
let is_empty = output.is_empty();
2020

21-
println!("{}", if is_empty { "Not solved." } else { &output });
21+
println!("{}", if is_empty { "Not solved." } else { &output.trim() });
2222

2323
if is_empty {
2424
0_f64

0 commit comments

Comments
 (0)