#[path = "../lib.rs"] mod lib; fn get_map() -> (Vec>, (usize, usize), (usize, usize)) { let mut heights = vec![]; let mut start = (0, 0); let mut end = (0, 0); for line in lib::iter_input() { let mut row = vec![]; for b in line.bytes() { match b { b'S' => { start = (row.len(), heights.len()); row.push(b'a'); }, b'E' => { end = (row.len(), heights.len()); row.push(b'z'); }, x => { row.push(x); }, } } heights.push(row); } return (heights, start, end); } fn get_siblings(x: usize, y: usize, rows: usize, cols: usize) -> Vec<(usize, usize)> { let mut result = vec![]; if x > 0 { result.push((x - 1, y)); } if x + 1 < cols { result.push((x + 1, y)); } if y > 0 { result.push((x, y - 1)); } if y + 1 < rows { result.push((x, y + 1)); } return result; } fn main() { let (heights, start, end) = get_map(); let rows = heights.len(); let cols = heights[0].len(); let mut queue = vec![end]; let mut m: Vec> = heights.iter().map(|row| row.iter().map(|_| u64::MAX - 1).collect() ).collect(); m[end.1][end.0] = 0; while let Some((x, y)) = queue.pop() { for (x1, y1) in get_siblings(x, y, rows, cols).into_iter() { if heights[y1][x1] + 1 >= heights[y][x] && m[y1][x1] > m[y][x] + 1 { m[y1][x1] = m[y][x] + 1; queue.push((x1, y1)); } } } println!("part1: {}", m[start.1][start.0]); let mut min_a_distance = u64::MAX; for y in 0..rows { for x in 0..cols { if heights[y][x] == b'a' && m[y][x] < min_a_distance { min_a_distance = m[y][x]; } } } println!("part2: {}", min_a_distance); }