From efa52f4f5b7c98f58ffd792f9f75407cabfd13b2 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Mon, 2 Dec 2024 21:50:56 +0100 Subject: [PATCH] changed the macro to support an Variable name --- .gitignore | 4 +++- data/2023/01a.txt | 1 + data/2023/01b.txt | 1 + macros/src/lib.rs | 23 +++++++++++++++++++---- src/bin/2023/01.rs | 11 +++++------ src/bin/2023/02.rs | 18 ++++++++++++------ src/bin/2024/01.rs | 2 +- src/bin/2024/02.rs | 2 +- 8 files changed, 43 insertions(+), 19 deletions(-) create mode 120000 data/2023/01a.txt create mode 120000 data/2023/01b.txt diff --git a/.gitignore b/.gitignore index 53c2147..a092d7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /target -/data +/data/**/*.txt +!/data/2023/01a.txt +!/data/2023/01b.txt !/data/20*/.gitkeep \ No newline at end of file diff --git a/data/2023/01a.txt b/data/2023/01a.txt new file mode 120000 index 0000000..f69f30e --- /dev/null +++ b/data/2023/01a.txt @@ -0,0 +1 @@ +01.txt \ No newline at end of file diff --git a/data/2023/01b.txt b/data/2023/01b.txt new file mode 120000 index 0000000..f69f30e --- /dev/null +++ b/data/2023/01b.txt @@ -0,0 +1 @@ +01.txt \ No newline at end of file diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 598f090..b391bbd 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -5,7 +5,7 @@ use quote::quote; use syn::{ buffer::Cursor, parse::{Parse, ParseStream, StepCursor}, - Error, + Error, LitStr, }; use syn::{parse, LitInt, Result}; @@ -14,6 +14,7 @@ const EXAMPLE_DIR: &str = "examples"; #[derive(Debug, Clone)] struct IncludeData { + var: String, year: String, day: String, } @@ -44,10 +45,20 @@ fn get_string<'a, 'b>(cursor: StepCursor<'a, 'b>) -> Result<(String, Cursor<'a>) impl Parse for IncludeData { fn parse(input: ParseStream) -> Result { let mut data = IncludeData { + var: "".into(), year: "".into(), day: "".into(), }; let mut look = input.lookahead1(); + if !look.peek(LitStr) { + let s = input.span(); + return Err(Error::new( + s, + format!("{} is not an valid Token", s.source_text().unwrap()), + )); + } + data.var = input.step(get_string).expect("Wanted a string"); + look = input.lookahead1(); if !look.peek(LitInt) { let s = input.span(); return Err(Error::new( @@ -96,7 +107,7 @@ fn canonicalize(dir: &str, input: Result) -> Option { /// includes Data from Advent of code /// ```ignore (cannot-doctest-external-file-dependency) -/// include_data!(2015 01) +/// include_data!(DATA 2015 01) /// /// fn main() { /// print!("{DATA}"); @@ -109,17 +120,21 @@ fn canonicalize(dir: &str, input: Result) -> Option { #[proc_macro] pub fn include_data(data: TokenStream) -> TokenStream { let input = parse::(data); + let name = match input.clone() { + Ok(d) => d.var, + Err(_)=>"DATA".into(), + }; let mut path = canonicalize(DATA_DIR, input.clone()); if path.is_none() { path = canonicalize(EXAMPLE_DIR, input) } match path { Some(p) => quote! { - static DATA: &str = include_str!(#p); + static #name: &str = include_str!(#p); } .into(), None => quote! { - static DATA: &str = ""; + static #name: &str = ""; } .into(), } diff --git a/src/bin/2023/01.rs b/src/bin/2023/01.rs index b8beef8..597822d 100644 --- a/src/bin/2023/01.rs +++ b/src/bin/2023/01.rs @@ -1,14 +1,13 @@ use advent_of_code::strings::{get_numbers, get_string_numbers}; #[allow(unused_imports)] -use adventofcode_macros::{include_example, include_example}; +use adventofcode_macros::{include_data, include_example}; -include_example!(2023 01); -//static DATA: &str; +include_data!(DATAa 2023 01a); +include_data!(DATAb 2023 01b); fn main() { let mut sum: u32 = 0; - let data: String = DATA.into(); - for coordinates in data.split('\n').map(get_numbers) { + for coordinates in DATAa.split('\n').map(get_numbers) { if coordinates.len() == 0 { continue; } @@ -20,7 +19,7 @@ fn main() { } println!("Sum of Numbers {}", sum); sum = 0; - for coordinates in data.split('\n').map(get_string_numbers) { + for coordinates in DATAb.split('\n').map(get_string_numbers) { if coordinates.len() == 0 { continue; } diff --git a/src/bin/2023/02.rs b/src/bin/2023/02.rs index 493433e..b5ca496 100644 --- a/src/bin/2023/02.rs +++ b/src/bin/2023/02.rs @@ -1,8 +1,8 @@ use advent_of_code::strings::parsenumber; #[allow(unused_imports)] -use adventofcode_macros::{include_example, include_example}; +use adventofcode_macros::{include_data, include_example}; -include_example!(2023 02); +include_example!(DATA 2023 02); //static DATA: &str; #[derive(Debug, Clone, Copy)] @@ -43,17 +43,23 @@ impl Round { ok } fn power(&self) -> u64 { - let mut sack = Sack{reds:0, greens:0, blues:0}; + let mut sack = Sack { + reds: 0, + greens: 0, + blues: 0, + }; for take in self.takes.clone() { if take.red > sack.reds { sack.reds = take.red - }if take.green > sack.greens { + } + if take.green > sack.greens { sack.greens = take.green - }if take.blue > sack.blues { + } + if take.blue > sack.blues { sack.blues = take.blue } } - sack.reds as u64 *sack.greens as u64*sack.blues as u64 + sack.reds as u64 * sack.greens as u64 * sack.blues as u64 } } diff --git a/src/bin/2024/01.rs b/src/bin/2024/01.rs index 2ee455e..6ceb7cf 100644 --- a/src/bin/2024/01.rs +++ b/src/bin/2024/01.rs @@ -2,7 +2,7 @@ use advent_of_code::strings::{parsenumber, splitspace}; #[allow(unused_imports)] use adventofcode_macros::{include_data, include_example}; -include_data!(2024 01); +include_data!(DATA 2024 01); fn distance(leftlist: &Vec, rightlist: &Vec) -> u32 { let mut distance = 0; diff --git a/src/bin/2024/02.rs b/src/bin/2024/02.rs index 7c267d5..0953474 100644 --- a/src/bin/2024/02.rs +++ b/src/bin/2024/02.rs @@ -4,7 +4,7 @@ use advent_of_code::strings::parsenumber; #[allow(unused_imports)] use adventofcode_macros::{include_data, include_example}; -include_data!(2024 02); +include_data!(DATA 2024 02); fn get_numbers(input: &str) -> Vec { input.split(char::is_whitespace).map(parsenumber).collect()