From 6c1064a3e3be957ed2d6b6529384abbe75e2ba43 Mon Sep 17 00:00:00 2001 From: Pau Costa Date: Fri, 15 Dec 2023 18:59:58 +0100 Subject: [PATCH] partial implementation of day 3 part 1 in rust --- rust/src/day3/mod.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++ rust/src/main.rs | 2 ++ 2 files changed, 76 insertions(+) create mode 100644 rust/src/day3/mod.rs diff --git a/rust/src/day3/mod.rs b/rust/src/day3/mod.rs new file mode 100644 index 0000000..87ffd23 --- /dev/null +++ b/rust/src/day3/mod.rs @@ -0,0 +1,74 @@ +use std::fs; + +pub fn run_day3(){ + let contents = fs::read_to_string("input.txt") + .expect("Could not load file"); + let part_one_result = part_one(&contents); +} + +struct SymbolPosition<'a> { + x: i32, + y: i32, + char_map: &'a Vec> +} + +impl SymbolPosition { + fn explore_neighbours()-> 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}, + ]; + + for neighbour in neighbours{ + if neighbour.is_digit(){ + found_numbers.push(neighbour.find_full_number()) + } + } + found_numbers + } + + fn is_digit()->bool{ + return regex::Regex::new(r"\d") + .unwrap() + .is_match(Self.char_map[Self.y][Self.x]); + } + + fn find_full_number()->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!() + } +} + +fn part_one(contents: &String)-> i32{ + let mut char_map: Vec> = Vec::new(); + let mut symbols: Vec = Vec::new(); + + for line in contents.lines(){ + let line_vec: Vec = line.chars().collect(); + char_map.push(line_vec); + } + + for (y, line) in char_map.iter().enumerate(){ + 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}) + } + } + } + + + + 0 +} diff --git a/rust/src/main.rs b/rust/src/main.rs index 3924e96..61177d6 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,5 +1,7 @@ mod day1; mod day2; +mod day3; + fn main() { day1::run_day1();