Adds Days 4 and 5.1

This commit is contained in:
2024-12-08 13:59:34 +01:00
parent 33d96510d3
commit e6c4b4ea7c
10 changed files with 1791 additions and 28 deletions

View File

@@ -10,4 +10,4 @@ proc-macro = true
syn = "2.0"
quote = "1.0"
darling = "0.20"
regex = "1.11"

View File

@@ -3,7 +3,8 @@ extern crate proc_macro;
use proc_macro::{TokenStream, TokenTree};
use quote::quote;
use syn::{parse_macro_input, parse_quote, Expr, ItemStruct};
use regex::Regex;
use syn::{parse_macro_input, Expr, Ident, ItemStruct};
#[proc_macro_attribute]
pub fn advent_day(attrs: TokenStream, item: TokenStream, ) -> TokenStream {
@@ -18,22 +19,50 @@ pub fn advent_day(attrs: TokenStream, item: TokenStream, ) -> TokenStream {
}
}
let call1 = format!("{}::new()", parts[0]);
let call2 = format!("{}::new()", parts[1]);
let part1: Expr = parse_quote!(#call1);
let part2: Expr = parse_quote!(#call2);
println!("{:?} {:?}", part1, part2);
let call1 = format!("{}::new()", &parts[0]);
let call2 = format!("{}::new()", &parts[1]);
let part1: Expr = syn::parse_str(&call1.as_str()).unwrap();
let part2: Expr = syn::parse_str(&call2.as_str()).unwrap();
let ident_part1: Ident = syn::parse_str(&parts[0].as_str()).unwrap();
let ident_part2: Ident = syn::parse_str(&parts[1].as_str()).unwrap();
let re = Regex::new(r"(\d+)").unwrap();
let hay = input.ident.to_string();
let (_, [day_nr]) = re.captures(hay.as_str()).unwrap().extract();
let input_str = format!("resources/input{}.txt", day_nr);
let output = quote! {
impl AdventDayPart1 for #ident_part1 {
fn read_input(&mut self) -> Result<(), AdventError> {
read_input_part1(self, Path::new(#input_str))
}
fn solve(&mut self) -> Result<u64, AdventError> {
solve_part1(self)
}
}
impl AdventDayPart2 for #ident_part2 {
fn read_input(&mut self) -> Result<(), AdventError> {
read_input_part2(self, Path::new(#input_str))
}
fn solve(&mut self) -> Result<u64, AdventError> {
solve_part2(self)
}
}
pub struct #ident {
part1: Box::<dyn AdventDayPart1>,
part2: Box::<dyn AdventDayPart2>
}
impl Day4 {
impl #ident {
pub fn new() -> Self {
Day4 {
Self {
part1: Box::new(#part1),
part2: Box::new(#part2)
}