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
/data
/data/**/*.txt
!/data/2023/01a.txt
!/data/2023/01b.txt
!/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::{
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<Self> {
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<IncludeData>) -> Option<String> {
/// 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<IncludeData>) -> Option<String> {
#[proc_macro]
pub fn include_data(data: TokenStream) -> TokenStream {
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());
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(),
}

Datei anzeigen

@ -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;
}

Datei anzeigen

@ -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,13 +43,19 @@ 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
}
}

Datei anzeigen

@ -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<u32>, rightlist: &Vec<u32>) -> u32 {
let mut distance = 0;

Datei anzeigen

@ -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<u32> {
input.split(char::is_whitespace).map(parsenumber).collect()