partial implementation of day 3 part 1 in rust

main
Pau Costa 2023-12-15 18:59:58 +01:00
parent c132d251cc
commit 6c1064a3e3
2 changed files with 76 additions and 0 deletions

74
rust/src/day3/mod.rs Normal file
View File

@ -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<Vec<char>>
}
impl SymbolPosition {
fn explore_neighbours()-> Vec<i32>{
let mut found_numbers: Vec<i32> = 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<char>> = Vec::new();
let mut symbols: Vec<SymbolPosition> = Vec::new();
for line in contents.lines(){
let line_vec: Vec<char> = 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(&current_char.to_string()){
symbols.push(SymbolPosition{x: x as i32, y: y as i32 , char_map: &char_map})
}
}
}
0
}

View File

@ -1,5 +1,7 @@
mod day1; mod day1;
mod day2; mod day2;
mod day3;
fn main() { fn main() {
day1::run_day1(); day1::run_day1();