diff --git a/src/day1.rs b/src/day1.rs index df60be6..c7810ed 100644 --- a/src/day1.rs +++ b/src/day1.rs @@ -1,7 +1,7 @@ use std::collections::VecDeque; /// 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 count: u32 = 0; 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) /// /// 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 count = 0; diff --git a/src/main.rs b/src/main.rs index 021221d..c30d094 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,29 +36,7 @@ fn main() { let input = read_input(day).unwrap(); // TODO macro-ify - match day { - 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), - } + match_days!(1, day1, 2, day2, 3, day3, 4, day4); } // TODO do the inputs ever change inside of a day? @@ -66,3 +44,19 @@ fn main() { fn read_input(day: u8) -> std::io::Result { 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), + } + }; +}