From 8b6029e304259f53454f03bf1bbc0c581eca1fc5 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Wed, 17 Jul 2024 10:55:37 +0200 Subject: [PATCH] formatting and cleanup --- bin/src/macros.rs | 33 +++++++++++++++++++++++++++++++++ bin/src/main.rs | 35 +++++------------------------------ 2 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 bin/src/macros.rs diff --git a/bin/src/macros.rs b/bin/src/macros.rs new file mode 100644 index 0000000..b53464c --- /dev/null +++ b/bin/src/macros.rs @@ -0,0 +1,33 @@ +#[macro_export] +macro_rules! time { + ($name:expr, $block:block) => {{ + let __start = std::time::Instant::now(); + { + $block + }; + let __duration = __start.elapsed(); + println!("[TIMING] '{}' took: {:?}", $name, __duration); + }}; + + ($name:expr, $fn:ident) => { + time!($name, { $fn() }) + }; +} + +#[macro_export] +macro_rules! day { + ($day:tt, $fn:ident) => { + time!(format!("Day {}", $day), { + println!("# Day {}", $day); + $fn(get_input!($day)); + }); + println!("-----"); + }; +} + +#[macro_export] +macro_rules! get_input { + ($day:tt) => { + std::fs::read_to_string(&format!("inputs/day{}.txt", $day)).expect("Couldn't read input-file!") + }; +} diff --git a/bin/src/main.rs b/bin/src/main.rs index 2440980..a02c7e3 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -1,7 +1,12 @@ #![allow(clippy::missing_panics_doc, missing_docs, clippy::missing_errors_doc)] +// Macros +mod macros; + +// Imports use std::str::FromStr; +// Solution imports use almanac::Almanac; use boat_race::Races; use camel_cards::Hands; @@ -12,36 +17,6 @@ use oasis::Report; use scratchcard::ScratchCards; use trebuchet::Trebuchet; -macro_rules! time { - ($name:expr, $block:block) => {{ - let __start = std::time::Instant::now(); - { - $block - }; - let __duration = __start.elapsed(); - println!("[TIMING] '{}' took: {:?}", $name, __duration); - }}; - - ($name:expr, $fn:ident) => { - time!($name, { $fn() }) - }; -} - -macro_rules! day { - ($day:tt, $fn:ident) => { - time!(format!("Day {}", $day), { - println!("# Day {}", $day); - $fn(get_input!($day)); - }) - }; -} - -macro_rules! get_input { - ($day:tt) => { - std::fs::read_to_string(&format!("inputs/day{}.txt", $day)).expect("Couldn't read input-file!") - }; -} - fn day1(input: String) { let result = Trebuchet::new(&input) .expect("Failed to create trebuchet")