Skip to content

Commit 62bb12c

Browse files
authored
refactor: dry solution! macro implementation (#48)
1 parent a79ce7a commit 62bb12c

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/template/mod.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,23 @@ pub fn read_file_part(folder: &str, day: Day, part: u8) -> String {
3939
#[macro_export]
4040
macro_rules! solution {
4141
($day:expr) => {
42-
/// The current day.
43-
const DAY: advent_of_code::template::Day = advent_of_code::day!($day);
44-
45-
fn main() {
46-
use advent_of_code::template::runner::*;
47-
let input = advent_of_code::template::read_file("inputs", DAY);
48-
run_part(part_one, &input, DAY, 1);
49-
run_part(part_two, &input, DAY, 2);
50-
}
42+
$crate::solution!(@impl $day, [part_one, 1] [part_two, 2]);
5143
};
5244
($day:expr, 1) => {
53-
/// Allows solving part one in isolation
54-
const DAY: advent_of_code::template::Day = advent_of_code::day!($day);
55-
56-
fn main() {
57-
use advent_of_code::template::runner::*;
58-
let input = advent_of_code::template::read_file("inputs", DAY);
59-
run_part(part_one, &input, DAY, 1);
60-
}
45+
$crate::solution!(@impl $day, [part_one, 1]);
6146
};
6247
($day:expr, 2) => {
63-
/// Allows solving part two in isolation
64-
const DAY: advent_of_code::template::Day = advent_of_code::day!($day);
48+
$crate::solution!(@impl $day, [part_two, 2]);
49+
};
50+
51+
(@impl $day:expr, $( [$func:expr, $part:expr] )*) => {
52+
/// The current day.
53+
const DAY: $crate::template::Day = $crate::day!($day);
6554

6655
fn main() {
67-
use advent_of_code::template::runner::*;
68-
let input = advent_of_code::template::read_file("inputs", DAY);
69-
run_part(part_two, &input, DAY, 2);
56+
use $crate::template::runner::*;
57+
let input = $crate::template::read_file("inputs", DAY);
58+
$( run_part($func, &input, DAY, $part); )*
7059
}
7160
};
7261
}

0 commit comments

Comments
 (0)