Trying to do advent of code in Rust. I am very new to rust so please help if you see me doing something stupid!!
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

started work on day 4

+130
+7
day4/Cargo.lock
··· 1 + # This file is automatically @generated by Cargo. 2 + # It is not intended for manual editing. 3 + version = 4 4 + 5 + [[package]] 6 + name = "day4" 7 + version = "0.1.0"
+6
day4/Cargo.toml
··· 1 + [package] 2 + name = "day4" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies]
+10
day4/shortinput.txt
··· 1 + ..@@.@@@@. 2 + @@@.@.@.@@ 3 + @@@@@.@.@@ 4 + @.@@@@..@. 5 + @@.@@@@.@@ 6 + .@@@@@@@.@ 7 + .@.@.@.@@@ 8 + @.@@@.@@@@ 9 + .@@@@@@@@. 10 + @.@.@@@.@.
+107
day4/src/main.rs
··· 1 + use std::fmt::Display; 2 + use std::ops::{Add, Sub}; 3 + 4 + struct Warehouse { 5 + layout: Vec<Vec<Option<char>>>, 6 + width: u16, 7 + height: u16, 8 + } 9 + 10 + impl Warehouse { 11 + fn new(width: u16, height: u16) -> Warehouse { 12 + Warehouse { 13 + layout: vec![vec![None; width as usize]; height as usize], 14 + width: width, 15 + height: height, 16 + } 17 + } 18 + 19 + fn get(&self, x: u16, y: u16) -> Option<char> { 20 + *self.layout.get(y as usize)?.get(x as usize)? 21 + } 22 + 23 + fn set(&mut self, x: u16, y: u16, value: char) { 24 + // TODO : This is not a complete check, could cause issues 25 + if y < self.layout.len() as u16 && x < self.layout[0].len() as u16 { 26 + self.layout[y as usize][x as usize] = Some(value); 27 + } 28 + } 29 + 30 + fn load_from_file(filename: &str) -> Warehouse { 31 + let contents = std::fs::read_to_string(filename).expect("Failed to read file"); 32 + let width = contents.lines().next().unwrap().len() as u16; 33 + let height = contents.lines().count() as u16; 34 + let layout = contents 35 + .lines() 36 + .map(|line| line.chars().map(|c| Some(c)).collect()) 37 + .collect(); 38 + Warehouse { 39 + layout, 40 + width, 41 + height, 42 + } 43 + } 44 + 45 + fn count_neighbors(&self, x: u16, y: u16) -> Option<u8> { 46 + println!("Counting neighbors at ({}, {})", x, y); 47 + let mut neighbors = 0u8; 48 + for n_y in y.checked_sub(1).unwrap_or(0)..y.add(2).min(self.height) { 49 + for n_x in x.checked_sub(1).unwrap_or(0)..x.add(2).min(self.width) { 50 + if n_y == y && n_x == x { 51 + continue; 52 + } 53 + if let Some(cell) = self.get(n_x, n_y) { 54 + if cell == '@' { 55 + neighbors += 1; 56 + println!("Neighbor found at ({}, {})", n_x, n_y); 57 + } 58 + } 59 + } 60 + } 61 + Some(neighbors) 62 + } 63 + } 64 + 65 + impl Display for Warehouse { 66 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 67 + for row in &self.layout { 68 + for cell in row { 69 + match cell { 70 + Some(c) => write!(f, "{}", c)?, 71 + None => write!(f, ".")?, 72 + } 73 + } 74 + writeln!(f)?; 75 + } 76 + Ok(()) 77 + } 78 + } 79 + 80 + fn main() { 81 + // let printer_department = Warehouse::new(10, 10); 82 + let printer_department = Warehouse::load_from_file("shortinput.txt"); 83 + let mut accessible_rolls = 0; 84 + // DONT LIKE THESE CLONES!! 85 + // Need to figure out how to make an iterator out of a reference 86 + for (x, row) in printer_department.layout.clone().into_iter().enumerate() { 87 + for (y, cell) in row.clone().into_iter().enumerate() { 88 + match cell { 89 + Some(c) => { 90 + if printer_department 91 + .count_neighbors(x as u16, y as u16) 92 + .unwrap_or(0) 93 + < 4 94 + { 95 + accessible_rolls += 1; 96 + println!("Accessible roll found at ({}, {})", x, y); 97 + } 98 + } 99 + None => continue, 100 + } 101 + } 102 + } 103 + println!("There are {} accessible rolls", accessible_rolls); 104 + let neighbors = printer_department.count_neighbors(0, 0); 105 + println!("Neighbors at (0, 0): {}", neighbors.unwrap()); 106 + println!("{}", printer_department); 107 + }