🐛 fixed is_digit and explore_neighbours memory access

main
Pau Costa 2023-12-15 19:45:47 +01:00
parent 6c1064a3e3
commit 281efda496
1 changed files with 29 additions and 18 deletions

View File

@ -7,46 +7,55 @@ pub fn run_day3(){
} }
struct SymbolPosition<'a> { struct SymbolPosition<'a> {
x: i32, x: usize,
y: i32, y: usize,
char_map: &'a Vec<Vec<char>> char_map: &'a Vec<Vec<char>>
} }
impl SymbolPosition { impl SymbolPosition<'_> {
fn explore_neighbours()-> Vec<i32>{ fn explore_neighbours(&self) -> Vec<i32>{
let mut found_numbers: Vec<i32> = Vec::new(); let mut found_numbers: Vec<i32> = Vec::new();
let neighbours: [SymbolPosition; 8] = [ let neighbours: [SymbolPosition; 8] = [
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 , 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 - 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 , 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 - 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 , 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},
]; ];
for neighbour in neighbours{ for neighbour in neighbours{
if neighbour.is_digit(){ if neighbour.is_digit(){
println!("Quack");
found_numbers.push(neighbour.find_full_number()) found_numbers.push(neighbour.find_full_number())
} }
} }
found_numbers found_numbers
} }
fn is_digit()->bool{ fn is_digit(&self)->bool{
return regex::Regex::new(r"\d") 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() .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 // Implement searching for a full number, mainly traverse the
// self.y position in the char_map forwards and backwards until // self.y position in the char_map forwards and backwards until
// we find the end of the vec, or a non digit character. Preserving // we find the end of the vec, or a non digit character. Preserving
// order. // order.
panic!() 0
} }
} }
@ -63,11 +72,13 @@ fn part_one(contents: &String)-> i32{
for (x, current_char) in line.iter().enumerate(){ for (x, current_char) in line.iter().enumerate(){
let non_digit = regex::Regex::new(r"[^\w.]").unwrap(); let non_digit = regex::Regex::new(r"[^\w.]").unwrap();
if non_digit.is_match(&current_char.to_string()){ if non_digit.is_match(&current_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 0