From b561e148e01b94452abeb45cfaa4e63166671331 Mon Sep 17 00:00:00 2001 From: mat ess Date: Sun, 4 Dec 2022 19:20:16 -0500 Subject: [PATCH] Add solutions macro --- src/main.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index bdeff1e..3f072e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,26 +6,19 @@ use anyhow::Error; use chrono::{Datelike, Local}; use pico_args::Arguments; -fn main() -> Result<(), Error> { - let mut args = Arguments::from_env(); - let small = args.contains("--small"); - let part = args.opt_free_from_str()?.unwrap_or(1); - let day = args - .opt_free_from_str()? - .unwrap_or_else(|| Local::now().day()); - let small = if small { ".small" } else { "" }; - let path = format!("inputs/{day}{small}.txt"); - let input = fs::read_to_string(path)?; - println!("running day {day} part {part}"); - let result = SOLNS[day as usize - 1][part as usize - 1](input); - println!("{result}"); - Ok(()) +macro_rules! solutions { + ($($all:tt),*) => { + const DAYS: usize = 0 $( + solutions!(@expand $all 1) )*; + const SOLUTIONS: [Day; DAYS] = [ + $($all),* + ]; + }; + (@expand $ignore:tt $e:expr) => {$e}; } type Part = fn(String) -> u64; type Day = [Part; 2]; -const DAYS: usize = 4; -const SOLNS: [Day; DAYS] = [ +solutions! { [ // day 1 part 1 |input| { @@ -136,8 +129,24 @@ const SOLNS: [Day; DAYS] = [ .map(|b| if b { 1 } else { 0 }) .sum() }, - ], -]; + ] +} + +fn main() -> Result<(), Error> { + let mut args = Arguments::from_env(); + let small = args.contains("--small"); + let part = args.opt_free_from_str()?.unwrap_or(1); + let day = args + .opt_free_from_str()? + .unwrap_or_else(|| Local::now().day()); + let small = if small { ".small" } else { "" }; + let path = format!("inputs/{day}{small}.txt"); + let input = fs::read_to_string(path)?; + println!("running day {day} part {part}"); + let result = SOLUTIONS[day as usize - 1][part as usize - 1](input); + println!("{result}"); + Ok(()) +} fn make_ranges(input: String) -> Vec<(Range, Range)> { input