Skip to content

Commit

Permalink
Improvement: Add solvable! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 1, 2024
1 parent e350abb commit ecd8976
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> $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<String, std::io::Error> {
Expand Down
50 changes: 19 additions & 31 deletions src/year_2015.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Option<String> {
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,
}
}
2 changes: 2 additions & 0 deletions src/year_2015/day_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ pub fn day_01_v2(input: impl Into<String>) -> i16 {
0
}

solvable!(day_01, day_01_v1, day_01_v2, i16);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub fn day_02_v2(input: impl Into<String>) -> u32 {
total
}

solvable!(day_02, day_02_v1, day_02_v2, u32);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub fn day_03_v2(input: impl Into<String>) -> usize {
return houses.len() as usize;
}

solvable!(day_03, day_03_v1, day_03_v2, usize);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub fn day_04_v2(input: impl Into<String>) -> u32 {
return loop_until_hash(clean_str, 0);
}

solvable!(day_04, day_04_v1, day_04_v2, u32);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
10 changes: 6 additions & 4 deletions src/year_2015/day_05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@ fn string_is_nice_v2(input: &str) -> bool {
return twice_pair && repeated;
}

pub fn day_05_v1(input: impl Into<String>) -> u32 {
pub fn day_05_v1(input: impl Into<String>) -> 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<String>) -> u32 {
pub fn day_05_v2(input: impl Into<String>) -> 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::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ pub fn day_06_v2(input: impl Into<String>) -> 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::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub fn day_07_v2(input: impl Into<String>) -> u32 {
return result;
}

solvable!(day_07, day_07_v1, day_07_v2, u32);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub fn day_08_v2(input: impl Into<String>) -> u16 {
total
}

solvable!(day_08, day_08_v1, day_08_v2, u16);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub fn day_09_v2<'a>(input: impl Into<String>) -> u16 {
best_path
}

solvable!(day_09, day_09_v1, day_09_v2, u16);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub fn day_10_v2(input: impl Into<String>) -> u32 {
next_input.len() as u32
}

solvable!(day_10, day_10_v1, day_10_v2, u32);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub fn day_11_v2(input: impl Into<String>) -> String {
return chr_to_string(&password);
}

solvable!(day_11, day_11_v1, day_11_v2, String);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub fn day_12_v2(input: impl Into<String>) -> i32 {
0
}

solvable!(day_12, day_12_v1, day_12_v2, i32);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ pub fn day_13_v2(input: impl Into<String>) -> i16 {
calculate_happiness(&paths, &0, friends_left, 0)
}

solvable!(day_13, day_13_v1, day_13_v2, i16);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub fn day_14_v2(input: impl Into<String>) -> 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::*;
Expand Down
2 changes: 2 additions & 0 deletions src/year_2015/day_15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ pub fn day_15_v2(input: impl Into<String>) -> 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::*;
Expand Down

0 comments on commit ecd8976

Please sign in to comment.