#[path = "../lib.rs"] mod lib; fn parse_input() -> Vec> { return lib::iter_input() .map(|line| line.bytes().collect()) .collect(); } fn add(x: usize, dx: isize, max: usize) -> Option { if dx > 0 { let y = x + (dx as usize); if y < max { return Some(y); } else { return None; } } else { let dy = -dx as usize; if x >= dy { return Some(x - dy); } else { return None; } } } fn get(map: &Vec>, x: usize, y: usize, dx: isize, dy: isize) -> Option { let xx = add(x, dx, map[0].len())?; let yy = add(y, dy, map.len())?; return Some(map[yy][xx]); } fn main() { let map = parse_input(); let h = map.len(); let w = map[0].len(); let mut count1 = 0; let mut count2 = 0; for y in 0..h { for x in 0..w { if map[y][x] == b'X' { for dx in -1..=1 { for dy in -1..=1 { if get(&map, x, y, dx, dy) == Some(b'M') && get(&map, x, y, 2 * dx, 2 * dy) == Some(b'A') && get(&map, x, y, 3 * dx, 3 * dy) == Some(b'S') { count1 += 1; } } } } else if map[y][x] == b'A' && ((get(&map, x, y, -1, -1) == Some(b'M') && get(&map, x, y, 1, 1) == Some(b'S')) || (get(&map, x, y, -1, -1) == Some(b'S') && get(&map, x, y, 1, 1) == Some(b'M'))) && ((get(&map, x, y, 1, -1) == Some(b'M') && get(&map, x, y, -1, 1) == Some(b'S')) || (get(&map, x, y, 1, -1) == Some(b'S') && get(&map, x, y, -1, 1) == Some(b'M'))) { count2 += 1; } } } println!("part1: {}", count1); println!("part2: {}", count2); }