#[path = "../lib.rs"] mod lib; fn parse_input() -> (Vec, usize) { let mut map = vec![]; let mut h = 0; for line in lib::iter_input() { h += 1; for c in line.chars() { map.push(c.to_digit(10).unwrap() as u8); } } return (map, h); } fn unique(a: &mut Vec) { a.sort(); let mut i = 1; while i < a.len() { if a[i] == a[i - 1] { a.remove(i); } else { i += 1; } } } fn find_next(positions: Vec, map: &Vec, h: usize) -> Vec { let w = map.len() / h; let mut result = vec![]; for i in positions.into_iter() { let v = map[i] - 1; if i / w != 0 && map[i - w] == v { result.push(i - w); } if i / w != h - 1 && map[i + w] == v { result.push(i + w); } if i % w != 0 && map[i - 1] == v { result.push(i - 1); } if i % w != w - 1 && map[i + 1] == v { result.push(i + 1); } } return result; } fn main() { let (map, h) = parse_input(); let mut sum1 = 0; let mut sum2 = 0; for (i, v) in map.iter().enumerate() { if *v == 9 { let mut positions = vec![i]; for _ in 0..9 { positions = find_next(positions, &map, h); } sum2 += positions.len(); unique(&mut positions); sum1 += positions.len(); } } println!("part1: {}", sum1); println!("part2: {}", sum2); }