Off the rails with a proc macro

This commit is contained in:
2024-12-06 15:13:27 +01:00
parent 6397ed0092
commit 33d96510d3
13 changed files with 494 additions and 5 deletions

View File

@@ -33,7 +33,6 @@ impl Report {
let mut new_vec = self.0.clone();
new_vec.remove(i);
let new_diff_vec : Vec<i32> = new_vec.iter().zip(new_vec.iter().skip(1)).map(|(l, r)| l - r).collect();
println!("{:?}", new_vec);
if new_diff_vec.iter().all(|x| {*x >= -3 && *x <= -1}) || new_diff_vec.iter().all(|x| *x >= 1 && *x <= 3) {
return true;
}

100
src/days/day3.rs Normal file
View File

@@ -0,0 +1,100 @@
use std::path::Path;
use regex::Regex;
use crate::input::get_input;
use super::*;
struct Mul {
l: i32,
r: i32,
}
struct Day3Part1(Vec<Mul>);
impl Day3Part1 {
fn new() -> Self {
Day3Part1(vec![])
}
}
impl AdventDayPart1 for Day3Part1 {
fn read_input(&mut self) -> Result<(),AdventError> {
let hay = get_input(Path::new("resources/input3.txt"));
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
for (_, [l, r]) in re.captures_iter(&hay).map(|c| c.extract())
{
self.0.push(Mul {l: l.parse::<i32>()?, r: r.parse::<i32>()?})
}
Ok(())
}
fn solve(&mut self) -> Result<u64, AdventError> {
Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result<u64,AdventError> {Ok(acc? + TryInto::<u64>::try_into(mul.l * mul.r)?)})?)
}
}
struct Day3Part2(Vec<Mul>);
impl Day3Part2 {
fn new() -> Self {
Day3Part2(vec![])
}
}
impl AdventDayPart2 for Day3Part2 {
fn read_input(&mut self) -> Result<(),AdventError> {
let hay = get_input(Path::new("resources/input3.txt"));
let re = Regex::new(r"mul\((\d+),(\d+)\)|don\'t\(\)|do\(\)").unwrap();
let mut state = 1;
for capture in re.captures_iter(&hay)
{
let str = capture.get(0).unwrap().as_str();
if str == "do()" {
state = 1;
continue;
}
if str == "don't()" {
state = 0;
continue;
}
if state == 1 {
let l = capture.get(1).unwrap().as_str();
let r = capture.get(2).unwrap().as_str();
self.0.push(Mul {l: l.parse::<i32>()?, r: r.parse::<i32>()?})
}
}
Ok(())
}
fn solve(&mut self) -> Result<u64, AdventError> {
Ok(self.0.iter().fold(Ok(0), |acc, mul| -> Result<u64,AdventError> {Ok(acc? + TryInto::<u64>::try_into(mul.l * mul.r)?)})?)
}
}
pub struct Day3 {
part1: Day3Part1,
part2: Day3Part2
}
impl Day3 {
pub fn new() -> Self {
Day3 {
part1: Day3Part1::new(),
part2: Day3Part2::new()
}
}
}
impl AdventDay for Day3 {
fn puzzle1(&mut self) -> Result<u64, crate::error::AdventError> {
self.part1.read_input()?;
self.part1.solve()
}
fn puzzle2(&mut self) -> Result<u64, crate::error::AdventError> {
self.part2.read_input()?;
self.part2.solve()
}
}

49
src/days/day4.rs Normal file
View File

@@ -0,0 +1,49 @@
use super::*;
use advent_derive::advent_day;
struct Day4Part1 {
}
impl Day4Part1 {
fn new() -> Self {
Day4Part1{}
}
}
struct Day4Part2 {
}
impl Day4Part2 {
fn new() -> Self {
Day4Part2{}
}
}
impl AdventDayPart1 for Day4Part1 {
fn read_input(&mut self) -> Result<(),AdventError> {
todo!()
}
fn solve(&mut self) -> Result<u64, AdventError> {
todo!()
}
}
impl AdventDayPart2 for Day4Part2 {
fn read_input(&mut self) -> Result<(),AdventError> {
todo!()
}
fn solve(&mut self) -> Result<u64, AdventError> {
todo!()
}
}
#[advent_day(Day4Part1, Day4Part2)]
struct Day4;

View File

@@ -16,4 +16,6 @@ pub trait AdventDay {
}
pub mod day1;
pub mod day2;
pub mod day2;
pub mod day3;
pub mod day4;

View File

@@ -26,4 +26,14 @@ pub fn get_lines(file_path: &Path) -> Lines<BufReader<File>> {
let buf = BufReader::new(file);
let lines = buf.lines();
lines
}
}
pub fn get_input(file_path: &Path) -> String {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push(file_path);
let file = File::open(path).expect("no such file");
let mut buf = BufReader::new(file);
let mut s: String = Default::default();
buf.read_to_string(&mut s).unwrap();
s
}

View File

@@ -4,9 +4,10 @@ pub mod days;
mod input;
use std::env;
use day1::Day1;
use day2::Day2;
use day3::Day3;
use day4::Day4;
use crate::error::AdventError;
use crate::days::*;
@@ -15,7 +16,9 @@ fn main() -> Result<(), AdventError> {
let mut advent_days: Vec<Box::<dyn AdventDay>> = vec!(
Box::new(Day1::new()),
Box::new(Day2::new())
Box::new(Day2::new()),
Box::new(Day3::new()),
Box::new(Day4::new()),
);
if env::args().len() == 1 {