changed the macro to support an Variable name

Dieser Commit ist enthalten in:
Sebastian Tobie 2024-12-02 21:50:56 +01:00
Ursprung 40d5411432
Commit efa52f4f5b
8 geänderte Dateien mit 43 neuen und 19 gelöschten Zeilen

4
.gitignore vendored
Datei anzeigen

@ -1,3 +1,5 @@
/target /target
/data /data/**/*.txt
!/data/2023/01a.txt
!/data/2023/01b.txt
!/data/20*/.gitkeep !/data/20*/.gitkeep

1
data/2023/01a.txt Softlink
Datei anzeigen

@ -0,0 +1 @@
01.txt

1
data/2023/01b.txt Softlink
Datei anzeigen

@ -0,0 +1 @@
01.txt

Datei anzeigen

@ -5,7 +5,7 @@ use quote::quote;
use syn::{ use syn::{
buffer::Cursor, buffer::Cursor,
parse::{Parse, ParseStream, StepCursor}, parse::{Parse, ParseStream, StepCursor},
Error, Error, LitStr,
}; };
use syn::{parse, LitInt, Result}; use syn::{parse, LitInt, Result};
@ -14,6 +14,7 @@ const EXAMPLE_DIR: &str = "examples";
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct IncludeData { struct IncludeData {
var: String,
year: String, year: String,
day: String, day: String,
} }
@ -44,10 +45,20 @@ fn get_string<'a, 'b>(cursor: StepCursor<'a, 'b>) -> Result<(String, Cursor<'a>)
impl Parse for IncludeData { impl Parse for IncludeData {
fn parse(input: ParseStream) -> Result<Self> { fn parse(input: ParseStream) -> Result<Self> {
let mut data = IncludeData { let mut data = IncludeData {
var: "".into(),
year: "".into(), year: "".into(),
day: "".into(), day: "".into(),
}; };
let mut look = input.lookahead1(); 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) { if !look.peek(LitInt) {
let s = input.span(); let s = input.span();
return Err(Error::new( return Err(Error::new(
@ -96,7 +107,7 @@ fn canonicalize(dir: &str, input: Result<IncludeData>) -> Option<String> {
/// includes Data from Advent of code /// includes Data from Advent of code
/// ```ignore (cannot-doctest-external-file-dependency) /// ```ignore (cannot-doctest-external-file-dependency)
/// include_data!(2015 01) /// include_data!(DATA 2015 01)
/// ///
/// fn main() { /// fn main() {
/// print!("{DATA}"); /// print!("{DATA}");
@ -109,17 +120,21 @@ fn canonicalize(dir: &str, input: Result<IncludeData>) -> Option<String> {
#[proc_macro] #[proc_macro]
pub fn include_data(data: TokenStream) -> TokenStream { pub fn include_data(data: TokenStream) -> TokenStream {
let input = parse::<IncludeData>(data); let input = parse::<IncludeData>(data);
let name = match input.clone() {
Ok(d) => d.var,
Err(_)=>"DATA".into(),
};
let mut path = canonicalize(DATA_DIR, input.clone()); let mut path = canonicalize(DATA_DIR, input.clone());
if path.is_none() { if path.is_none() {
path = canonicalize(EXAMPLE_DIR, input) path = canonicalize(EXAMPLE_DIR, input)
} }
match path { match path {
Some(p) => quote! { Some(p) => quote! {
static DATA: &str = include_str!(#p); static #name: &str = include_str!(#p);
} }
.into(), .into(),
None => quote! { None => quote! {
static DATA: &str = ""; static #name: &str = "";
} }
.into(), .into(),
} }

Datei anzeigen

@ -1,14 +1,13 @@
use advent_of_code::strings::{get_numbers, get_string_numbers}; use advent_of_code::strings::{get_numbers, get_string_numbers};
#[allow(unused_imports)] #[allow(unused_imports)]
use adventofcode_macros::{include_example, include_example}; use adventofcode_macros::{include_data, include_example};
include_example!(2023 01); include_data!(DATAa 2023 01a);
//static DATA: &str; include_data!(DATAb 2023 01b);
fn main() { fn main() {
let mut sum: u32 = 0; let mut sum: u32 = 0;
let data: String = DATA.into(); for coordinates in DATAa.split('\n').map(get_numbers) {
for coordinates in data.split('\n').map(get_numbers) {
if coordinates.len() == 0 { if coordinates.len() == 0 {
continue; continue;
} }
@ -20,7 +19,7 @@ fn main() {
} }
println!("Sum of Numbers {}", sum); println!("Sum of Numbers {}", sum);
sum = 0; 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 { if coordinates.len() == 0 {
continue; continue;
} }

Datei anzeigen

@ -1,8 +1,8 @@
use advent_of_code::strings::parsenumber; use advent_of_code::strings::parsenumber;
#[allow(unused_imports)] #[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; //static DATA: &str;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -43,17 +43,23 @@ impl Round {
ok ok
} }
fn power(&self) -> u64 { 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() { for take in self.takes.clone() {
if take.red > sack.reds { if take.red > sack.reds {
sack.reds = take.red sack.reds = take.red
}if take.green > sack.greens { }
if take.green > sack.greens {
sack.greens = take.green sack.greens = take.green
}if take.blue > sack.blues { }
if take.blue > sack.blues {
sack.blues = take.blue 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
} }
} }

Datei anzeigen

@ -2,7 +2,7 @@ use advent_of_code::strings::{parsenumber, splitspace};
#[allow(unused_imports)] #[allow(unused_imports)]
use adventofcode_macros::{include_data, include_example}; use adventofcode_macros::{include_data, include_example};
include_data!(2024 01); include_data!(DATA 2024 01);
fn distance(leftlist: &Vec<u32>, rightlist: &Vec<u32>) -> u32 { fn distance(leftlist: &Vec<u32>, rightlist: &Vec<u32>) -> u32 {
let mut distance = 0; let mut distance = 0;

Datei anzeigen

@ -4,7 +4,7 @@ use advent_of_code::strings::parsenumber;
#[allow(unused_imports)] #[allow(unused_imports)]
use adventofcode_macros::{include_data, include_example}; use adventofcode_macros::{include_data, include_example};
include_data!(2024 02); include_data!(DATA 2024 02);
fn get_numbers(input: &str) -> Vec<u32> { fn get_numbers(input: &str) -> Vec<u32> {
input.split(char::is_whitespace).map(parsenumber).collect() input.split(char::is_whitespace).map(parsenumber).collect()