---cargo --- static CONTENT: &'static str = include_str!("./inputs/07_1.txt"); #[derive(Debug, PartialEq)] enum Cell { Empty, Split, Sourc, TBeam, } fn main() { let mut grid: Vec<_> = CONTENT.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| { l.chars().map(|c| match c { '.' => Cell::Empty, 'S' => Cell::Sourc, '^' => Cell::Split, other => unreachable!("Unexpected Input {:?}", other), }).collect::>() }).collect(); println!("Grid: {:?}", grid); // (x, y) let source = grid.iter().enumerate().flat_map(|(y, row)| core::iter::repeat(y).zip(row.iter().enumerate()).map(|(y, (x, c))| (y, x, c))).find_map(|(y, x, c)| (c == &Cell::Sourc).then_some((x, y))).unwrap(); println!("Source {:?}", source); let mut splits = 0; let mut queue: Vec<(usize, usize)> = vec![(source.0, source.1+1)]; while let Some((x, mut y)) = queue.pop() { if grid[y][x] != Cell::Empty { continue; } while grid.get(y).map(|r| &r[x]) == Some(&Cell::Empty) { // println!("({}, {})", x, y); grid[y][x] = Cell::TBeam; y += 1; } match grid.get(y).map(|r| &r[x]) { None => {} Some(Cell::Empty) => unreachable!(), Some(Cell::Sourc) => unreachable!(), Some(Cell::Split) => { // println!("Split"); splits += 1; if let Some(nx) = x.checked_sub(1) { queue.push((nx, y)); } if x+1 < grid[y].len() { queue.push((x+1, y)); } } Some(Cell::TBeam) => { println!("Beam"); } }; } println!("Splits: {:?}", splits); }