From ecd8976011712fd5e0c9786d6490ca183f8418b4 Mon Sep 17 00:00:00 2001 From: red Date: Fri, 1 Mar 2024 02:30:09 +0100 Subject: [PATCH] Improvement: Add solvable! macro --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- src/lib.rs | 14 ++++++++++++ src/year_2015.rs | 50 ++++++++++++++++------------------------- src/year_2015/day_01.rs | 2 ++ src/year_2015/day_02.rs | 2 ++ src/year_2015/day_03.rs | 2 ++ src/year_2015/day_04.rs | 2 ++ src/year_2015/day_05.rs | 10 +++++---- src/year_2015/day_06.rs | 2 ++ src/year_2015/day_07.rs | 2 ++ src/year_2015/day_08.rs | 2 ++ src/year_2015/day_09.rs | 2 ++ src/year_2015/day_10.rs | 2 ++ src/year_2015/day_11.rs | 2 ++ src/year_2015/day_12.rs | 2 ++ src/year_2015/day_13.rs | 2 ++ src/year_2015/day_14.rs | 2 ++ src/year_2015/day_15.rs | 2 ++ 19 files changed, 72 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0c3d6d..8f5d08c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ Of note: - The changelog 2015.5.2 has been rewritten from each commit content. - This file may be amended entirely in the future to adhere to the [GNU Changelog style](https://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html#Style-of-Change-Logs) +## [2015.15.2] +### Added +- Added a (disappointingly) little macro to save time rewriting stuff. + ## [2015.15.1] ### Added - Solved [exercice for 2015, day 15](src/year_2015/day_15.rs). diff --git a/Cargo.toml b/Cargo.toml index 5cc0a4e..90ea938 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-rs" -version = "2015.15.1" +version = "2015.15.2" edition = "2021" authors = ["Arnaud 'red' Rouyer"] readme = "README.md" diff --git a/src/lib.rs b/src/lib.rs index 0e0742a..dc1d274 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,20 @@ use std::fs; use std::io; use std::path::PathBuf; +macro_rules! solvable { + ($day_xx:tt, $day_v1:tt, $day_v2:tt, $retype:ty) => { + pub fn $day_xx(part: u8, input: impl Into) -> $retype { + match part { + 1 => $day_v1(input), + 2 => $day_v2(input), + _ => { + panic!("Invalid part: {}", part) + } + } + } + }; +} + pub mod year_2015; pub fn fetch_input_from_file(filename: PathBuf) -> Result { diff --git a/src/year_2015.rs b/src/year_2015.rs index 4a909fc..d994710 100644 --- a/src/year_2015.rs +++ b/src/year_2015.rs @@ -38,37 +38,25 @@ pub mod day_15; /// assert_eq!(solution, Some("0".to_string())); /// ``` pub fn solve(day: u8, part: u8, input: impl Into) -> Option { - match (day, part) { - (1, 1) => return Some(format!("{}", day_01::day_01_v1(input))), - (1, 2) => return Some(format!("{}", day_01::day_01_v2(input))), - (2, 1) => return Some(format!("{}", day_02::day_02_v2(input))), - (2, 2) => return Some(format!("{}", day_02::day_02_v2(input))), - (3, 1) => return Some(format!("{}", day_03::day_03_v2(input))), - (3, 2) => return Some(format!("{}", day_03::day_03_v2(input))), - (4, 1) => return Some(format!("{}", day_04::day_04_v2(input))), - (4, 2) => return Some(format!("{}", day_04::day_04_v2(input))), - (5, 1) => return Some(format!("{}", day_05::day_05_v2(input))), - (5, 2) => return Some(format!("{}", day_05::day_05_v2(input))), - (6, 1) => return Some(format!("{}", day_06::day_06_v2(input))), - (6, 2) => return Some(format!("{}", day_06::day_06_v2(input))), - (7, 1) => return Some(format!("{}", day_07::day_07_v2(input))), - (7, 2) => return Some(format!("{}", day_07::day_07_v2(input))), - (8, 1) => return Some(format!("{}", day_08::day_08_v1(input))), - (8, 2) => return Some(format!("{}", day_08::day_08_v2(input))), - (9, 1) => return Some(format!("{}", day_09::day_09_v1(input))), - (9, 2) => return Some(format!("{}", day_09::day_09_v2(input))), - (10, 1) => return Some(format!("{}", day_10::day_10_v1(input))), - (10, 2) => return Some(format!("{}", day_10::day_10_v2(input))), - (11, 1) => return Some(format!("{}", day_11::day_11_v1(input))), - (11, 2) => return Some(format!("{}", day_11::day_11_v2(input))), - (12, 1) => return Some(format!("{}", day_12::day_12_v1(input))), - (12, 2) => return Some(format!("{}", day_12::day_12_v2(input))), - (13, 1) => return Some(format!("{}", day_13::day_13_v1(input))), - (13, 2) => return Some(format!("{}", day_13::day_13_v2(input))), - (14, 1) => return Some(format!("{}", day_14::day_14_v1(input))), - (14, 2) => return Some(format!("{}", day_14::day_14_v2(input))), - (15, 1) => return Some(format!("{}", day_15::day_15_v1(input))), - (15, 2) => return Some(format!("{}", day_15::day_15_v2(input))), + if part != 1 && part != 2 { + return None; + } + match day { + 1 => return Some(format!("{}", day_01::day_01(part, input))), + 2 => return Some(format!("{}", day_02::day_02(part, input))), + 3 => return Some(format!("{}", day_03::day_03(part, input))), + 4 => return Some(format!("{}", day_04::day_04(part, input))), + 5 => return Some(format!("{}", day_05::day_05(part, input))), + 6 => return Some(format!("{}", day_06::day_06(part, input))), + 7 => return Some(format!("{}", day_07::day_07(part, input))), + 8 => return Some(format!("{}", day_08::day_08(part, input))), + 9 => return Some(format!("{}", day_09::day_09(part, input))), + 10 => return Some(format!("{}", day_10::day_10(part, input))), + 11 => return Some(format!("{}", day_11::day_11(part, input))), + 12 => return Some(format!("{}", day_12::day_12(part, input))), + 13 => return Some(format!("{}", day_13::day_13(part, input))), + 14 => return Some(format!("{}", day_14::day_14(part, input))), + 15 => return Some(format!("{}", day_15::day_15(part, input))), _ => return None, } } diff --git a/src/year_2015/day_01.rs b/src/year_2015/day_01.rs index b767131..076935c 100644 --- a/src/year_2015/day_01.rs +++ b/src/year_2015/day_01.rs @@ -160,6 +160,8 @@ pub fn day_01_v2(input: impl Into) -> i16 { 0 } +solvable!(day_01, day_01_v1, day_01_v2, i16); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_02.rs b/src/year_2015/day_02.rs index 8efd40d..097509c 100644 --- a/src/year_2015/day_02.rs +++ b/src/year_2015/day_02.rs @@ -71,6 +71,8 @@ pub fn day_02_v2(input: impl Into) -> u32 { total } +solvable!(day_02, day_02_v1, day_02_v2, u32); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_03.rs b/src/year_2015/day_03.rs index 3428504..584bc2f 100644 --- a/src/year_2015/day_03.rs +++ b/src/year_2015/day_03.rs @@ -39,6 +39,8 @@ pub fn day_03_v2(input: impl Into) -> usize { return houses.len() as usize; } +solvable!(day_03, day_03_v1, day_03_v2, usize); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_04.rs b/src/year_2015/day_04.rs index 4a00501..c0097dc 100644 --- a/src/year_2015/day_04.rs +++ b/src/year_2015/day_04.rs @@ -29,6 +29,8 @@ pub fn day_04_v2(input: impl Into) -> u32 { return loop_until_hash(clean_str, 0); } +solvable!(day_04, day_04_v1, day_04_v2, u32); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_05.rs b/src/year_2015/day_05.rs index dc22086..1fadebc 100644 --- a/src/year_2015/day_05.rs +++ b/src/year_2015/day_05.rs @@ -45,22 +45,24 @@ fn string_is_nice_v2(input: &str) -> bool { return twice_pair && repeated; } -pub fn day_05_v1(input: impl Into) -> u32 { +pub fn day_05_v1(input: impl Into) -> usize { let input_str = input.into(); return input_str .lines() .filter(|line| string_is_nice_v1(line)) - .count() as u32; + .count() as usize; } -pub fn day_05_v2(input: impl Into) -> u32 { +pub fn day_05_v2(input: impl Into) -> usize { let input_str = input.into(); return input_str .lines() .filter(|line| string_is_nice_v2(line)) - .count() as u32; + .count() as usize; } +solvable!(day_05, day_05_v1, day_05_v2, usize); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_06.rs b/src/year_2015/day_06.rs index 4abf7d6..0159259 100644 --- a/src/year_2015/day_06.rs +++ b/src/year_2015/day_06.rs @@ -115,6 +115,8 @@ pub fn day_06_v2(input: impl Into) -> u32 { return light_grid.iter().map(|&i| i as u32).sum(); } +solvable!(day_06, day_06_v1, day_06_v2, u32); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_07.rs b/src/year_2015/day_07.rs index 82a48c8..8ccc6f5 100644 --- a/src/year_2015/day_07.rs +++ b/src/year_2015/day_07.rs @@ -99,6 +99,8 @@ pub fn day_07_v2(input: impl Into) -> u32 { return result; } +solvable!(day_07, day_07_v1, day_07_v2, u32); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_08.rs b/src/year_2015/day_08.rs index 5badc3f..fa3de90 100644 --- a/src/year_2015/day_08.rs +++ b/src/year_2015/day_08.rs @@ -48,6 +48,8 @@ pub fn day_08_v2(input: impl Into) -> u16 { total } +solvable!(day_08, day_08_v1, day_08_v2, u16); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_09.rs b/src/year_2015/day_09.rs index 50cc791..1ee9d45 100644 --- a/src/year_2015/day_09.rs +++ b/src/year_2015/day_09.rs @@ -112,6 +112,8 @@ pub fn day_09_v2<'a>(input: impl Into) -> u16 { best_path } +solvable!(day_09, day_09_v1, day_09_v2, u16); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_10.rs b/src/year_2015/day_10.rs index daf613c..afdaf0e 100644 --- a/src/year_2015/day_10.rs +++ b/src/year_2015/day_10.rs @@ -47,6 +47,8 @@ pub fn day_10_v2(input: impl Into) -> u32 { next_input.len() as u32 } +solvable!(day_10, day_10_v1, day_10_v2, u32); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_11.rs b/src/year_2015/day_11.rs index d7f8628..e10d4c8 100644 --- a/src/year_2015/day_11.rs +++ b/src/year_2015/day_11.rs @@ -83,6 +83,8 @@ pub fn day_11_v2(input: impl Into) -> String { return chr_to_string(&password); } +solvable!(day_11, day_11_v1, day_11_v2, String); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_12.rs b/src/year_2015/day_12.rs index 3c91303..e9cb998 100644 --- a/src/year_2015/day_12.rs +++ b/src/year_2015/day_12.rs @@ -125,6 +125,8 @@ pub fn day_12_v2(input: impl Into) -> i32 { 0 } +solvable!(day_12, day_12_v1, day_12_v2, i32); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_13.rs b/src/year_2015/day_13.rs index 86f029a..2051df7 100644 --- a/src/year_2015/day_13.rs +++ b/src/year_2015/day_13.rs @@ -111,6 +111,8 @@ pub fn day_13_v2(input: impl Into) -> i16 { calculate_happiness(&paths, &0, friends_left, 0) } +solvable!(day_13, day_13_v1, day_13_v2, i16); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_14.rs b/src/year_2015/day_14.rs index f8d547f..ca8e75f 100644 --- a/src/year_2015/day_14.rs +++ b/src/year_2015/day_14.rs @@ -98,6 +98,8 @@ pub fn day_14_v2(input: impl Into) -> u16 { reindeers.iter().map(|deer| deer.points).max().unwrap() } +solvable!(day_14, day_14_v1, day_14_v2, u16); + #[cfg(test)] mod tests { use super::*; diff --git a/src/year_2015/day_15.rs b/src/year_2015/day_15.rs index a230333..d5346a7 100644 --- a/src/year_2015/day_15.rs +++ b/src/year_2015/day_15.rs @@ -96,6 +96,8 @@ pub fn day_15_v2(input: impl Into) -> i32 { try_best_ingredient(&ingredients, &mut quantities, 0, &|c| c != 500) } +solvable!(day_15, day_15_v1, day_15_v2, i32); + #[cfg(test)] mod tests { use super::*;