macro-ify matching days and parts

This commit is contained in:
David Lenfesty 2021-12-04 12:36:18 -07:00
parent 0fb63e65e9
commit bf6b034768
2 changed files with 19 additions and 25 deletions

View File

@ -1,7 +1,7 @@
use std::collections::VecDeque; use std::collections::VecDeque;
/// Increments count if each sample is larger than the previous. /// Increments count if each sample is larger than the previous.
pub fn day1_p1(input: String) { pub fn part1(input: String) {
let mut prev = None; let mut prev = None;
let mut count: u32 = 0; let mut count: u32 = 0;
for line in input.lines() { for line in input.lines() {
@ -22,7 +22,7 @@ pub fn day1_p1(input: String) {
/// 3-sample windowing average (no need to average, just use sum here) /// 3-sample windowing average (no need to average, just use sum here)
/// ///
/// We need to start comparing when we have all of /// We need to start comparing when we have all of
pub fn day1_p2(input: String) { pub fn part2(input: String) {
let mut storage = VecDeque::with_capacity(5); let mut storage = VecDeque::with_capacity(5);
let mut count = 0; let mut count = 0;

View File

@ -36,29 +36,7 @@ fn main() {
let input = read_input(day).unwrap(); let input = read_input(day).unwrap();
// TODO macro-ify // TODO macro-ify
match day { match_days!(1, day1, 2, day2, 3, day3, 4, day4);
1 => match part {
1 => day1::day1_p1(input),
2 => day1::day1_p2(input),
_ => (),
},
2 => match part {
1 => day2::part1(input),
2 => day2::part2(input),
_ => (),
},
3 => match part {
1 => day3::part1(input),
2 => day3::part2(input),
_ => (),
},
4 => match part {
1 => day4::part1(input),
2 => day4::part2(input),
_ => (),
},
_ => println!("Day {} not completed yet!", day),
}
} }
// TODO do the inputs ever change inside of a day? // TODO do the inputs ever change inside of a day?
@ -66,3 +44,19 @@ fn main() {
fn read_input(day: u8) -> std::io::Result<String> { fn read_input(day: u8) -> std::io::Result<String> {
std::fs::read_to_string(format!("inputs/day{}", day)) std::fs::read_to_string(format!("inputs/day{}", day))
} }
#[macro_export]
macro_rules! match_days {
( $($num:expr, $day:ident),* ) => {
match day {
$(
$num => match part {
1 => $day::part1(input),
2 => $day::part2(input),
_ => println!("Part {} invalid!", part),
},
)*
_ => println!("Day {} not completed yet!", day),
}
};
}