pub fn answer(s : &str) -> u32 { let grid : Vec> = s.split("\n") .map(|s| s.chars().collect()) .collect(); let rows : i32 = grid.len().try_into().unwrap(); let cols : i32 = grid[0].len().try_into().unwrap(); let mut total : u32 = 0; for y in 0..rows { for x in 0..cols { if grid[y as usize][x as usize] == 'X' { total = total + inspect_location(&grid, x, y, rows, cols) } } }; total } fn inspect_location(grid : &Vec>, x : i32, y : i32, rows : i32, cols : i32) -> u32 { let mut total = 0; for dx in -1..=1 { for dy in -1..=1 { if dx == 0 && dy == 0 { continue; } let (s_x, s_y) = ( x + 3 * dx, y + 3 * dy ); if s_x < 0 || s_x >= rows || s_y < 0 || s_y >= cols { continue; } let m = grid[(y + 1 * dy) as usize][(x + 1 * dx) as usize]; let a = grid[(y + 2 * dy) as usize][(x + 2 * dx) as usize]; let s = grid[(y + 3 * dy) as usize][(x + 3 * dx) as usize]; if m == 'M' && a == 'A' && s == 'S' { total = total + 1; } } }; total }