From 281efda496698c0c6de9eb39cb1ee1bbc07ca859 Mon Sep 17 00:00:00 2001 From: Pau Costa Date: Fri, 15 Dec 2023 19:45:47 +0100 Subject: [PATCH] :bug: fixed is_digit and explore_neighbours memory access --- rust/src/day3/mod.rs | 47 +++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/rust/src/day3/mod.rs b/rust/src/day3/mod.rs index 87ffd23..c56d10b 100644 --- a/rust/src/day3/mod.rs +++ b/rust/src/day3/mod.rs @@ -7,46 +7,55 @@ pub fn run_day3(){ } struct SymbolPosition<'a> { - x: i32, - y: i32, + x: usize, + y: usize, char_map: &'a Vec> } -impl SymbolPosition { - fn explore_neighbours()-> Vec{ +impl SymbolPosition<'_> { + fn explore_neighbours(&self) -> Vec{ let mut found_numbers: Vec = Vec::new(); let neighbours: [SymbolPosition; 8] = [ - SymbolPosition{x: Self.x - 1, y: Self.y - 1, char_map: Self.char_map}, - SymbolPosition{x: Self.x , y: Self.y - 1, char_map: Self.char_map}, - SymbolPosition{x: Self.x + 1, y: Self.y - 1, char_map: Self.char_map}, - SymbolPosition{x: Self.x - 1, y: Self.y , char_map: Self.char_map}, - SymbolPosition{x: Self.x + 1, y: Self.y , char_map: Self.char_map}, - SymbolPosition{x: Self.x - 1, y: Self.y + 1, char_map: Self.char_map}, - SymbolPosition{x: Self.x , y: Self.y + 1, char_map: Self.char_map}, - SymbolPosition{x: Self.x + 1, y: Self.y + 1, char_map: Self.char_map}, + SymbolPosition{x: self.x - 1, y: self.y - 1, char_map: self.char_map}, + SymbolPosition{x: self.x , y: self.y - 1, char_map: self.char_map}, + SymbolPosition{x: self.x + 1, y: self.y - 1, char_map: self.char_map}, + SymbolPosition{x: self.x - 1, y: self.y , char_map: self.char_map}, + SymbolPosition{x: self.x + 1, y: self.y , char_map: self.char_map}, + SymbolPosition{x: self.x - 1, y: self.y + 1, char_map: self.char_map}, + SymbolPosition{x: self.x , y: self.y + 1, char_map: self.char_map}, + SymbolPosition{x: self.x + 1, y: self.y + 1, char_map: self.char_map}, ]; for neighbour in neighbours{ if neighbour.is_digit(){ + println!("Quack"); found_numbers.push(neighbour.find_full_number()) } } found_numbers } - fn is_digit()->bool{ - return regex::Regex::new(r"\d") + fn is_digit(&self)->bool{ + let self_char = &*match self.char_map.get(self.y) { + None => "".to_string(), + Some(char_vec) => match char_vec.get(self.x) { + None => "".to_string(), + Some(char) => char.to_string() + } + }; + + regex::Regex::new(r"\d") .unwrap() - .is_match(Self.char_map[Self.y][Self.x]); + .is_match(self_char) } - fn find_full_number()->i32{ + fn find_full_number(self)->i32{ // Implement searching for a full number, mainly traverse the // self.y position in the char_map forwards and backwards until // we find the end of the vec, or a non digit character. Preserving // order. - panic!() + 0 } } @@ -63,11 +72,13 @@ fn part_one(contents: &String)-> i32{ for (x, current_char) in line.iter().enumerate(){ let non_digit = regex::Regex::new(r"[^\w.]").unwrap(); if non_digit.is_match(¤t_char.to_string()){ - symbols.push(SymbolPosition{x: x as i32, y: y as i32 , char_map: &char_map}) + symbols.push(SymbolPosition{x, y , char_map: &char_map}) } } } + symbols[0].explore_neighbours(); + 0