Skip to content

Commit 6dd52dd

Browse files
committed
handle fallible solutions
1 parent 6a88197 commit 6dd52dd

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

src/parse.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ macro_rules! parse {
2929
// Empty rules
3030
( day $apply: ident, $ctx: tt, $val: expr; ) => {};
3131

32+
// Read fallible solution
33+
(
34+
sol $apply: ident, $ctx: tt, $val: expr;
35+
{ day $day: tt { $gen: tt { $( $acc: tt )* } } } ;
36+
$sol: ident ? $( $tail: tt )*
37+
) => {
38+
$crate::parse!(
39+
post_sol $apply, $ctx, $val;
40+
{ day $day { $gen { $( $acc )* { sol_fallible $sol } } } };
41+
$( $tail )*
42+
)
43+
};
44+
3245
// Read solution
3346
(
3447
sol $apply: ident, $ctx: tt, $val: expr;

src/run.rs

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
macro_rules! run_day {
33
(
44
{ $i: expr, $curr_day: expr, $year: expr, $opt: expr },
5-
{ day $day: ident { $gen: tt { $( { sol $solution: ident } )* } } }
5+
{ day $day: ident { $gen: tt { $( $sol: tt )* } } }
66
) => {{
77
use $crate::colored::*;
88

@@ -26,25 +26,9 @@ macro_rules! run_day {
2626
};
2727

2828
if let Some(input) = $crate::run_gen!($day, &data, $gen) {
29-
$({
30-
let start = Instant::now();
31-
let response = $day::$solution(&input);
32-
let elapsed = start.elapsed();
33-
34-
$crate::print_with_duration(
35-
stringify!($solution),
36-
Some(format!("{}", response).normal()),
37-
Some(elapsed),
38-
);
39-
})+
29+
$( $crate::run_sol!($day, &input, $sol); )+
4030
} else {
41-
$({
42-
$crate::print_with_duration(
43-
stringify!($solution),
44-
Some("skipped".dimmed()),
45-
None,
46-
);
47-
})+
31+
$( $crate::skip_sol!($sol); )+
4832
}
4933
}
5034
}}
@@ -87,3 +71,49 @@ macro_rules! run_gen {
8771
}
8872
}};
8973
}
74+
75+
#[macro_export]
76+
macro_rules! run_sol {
77+
// Run solution
78+
( $day: ident, $input: expr, { sol $solution: ident } ) => {{
79+
let start = Instant::now();
80+
let response = $day::$solution($input);
81+
let elapsed = start.elapsed();
82+
83+
$crate::print_with_duration(
84+
stringify!($solution),
85+
Some(format!("{}", response).normal()),
86+
Some(elapsed),
87+
);
88+
}};
89+
90+
// Run fallible solution
91+
( $day: ident, $input: expr, { sol_fallible $solution: ident } ) => {{
92+
use $crate::colored::*;
93+
use $crate::try_unwrap::TryUnwrap;
94+
95+
let start = Instant::now();
96+
let response = $day::$solution($input);
97+
let elapsed = start.elapsed();
98+
99+
match response.try_unwrap() {
100+
Ok(response) => {
101+
$crate::print_with_duration(
102+
stringify!($solution),
103+
Some(format!("{}", response).normal()),
104+
Some(elapsed),
105+
);
106+
}
107+
Err(msg) => {
108+
$crate::print_with_duration(stringify!($solution), Some(msg.red()), Some(elapsed));
109+
}
110+
}
111+
}};
112+
}
113+
114+
#[macro_export]
115+
macro_rules! skip_sol {
116+
({ $kind: tt $solution: ident }) => {{
117+
$crate::print_with_duration(stringify!($solution), Some("skipped".dimmed()), None);
118+
}};
119+
}

0 commit comments

Comments
 (0)