|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +fn take_step( |
| 4 | + garden: &HashSet<(usize, usize)>, |
| 5 | + w: usize, |
| 6 | + h: usize, |
| 7 | + positions: &HashSet<(usize, usize)>, |
| 8 | +) -> HashSet<(usize, usize)> { |
| 9 | + let mut new_positions: HashSet<(usize, usize)> = HashSet::new(); |
| 10 | + for pos in positions { |
| 11 | + if pos.0 > 0 && !garden.contains(&(pos.0 - 1, pos.1)) { |
| 12 | + new_positions.insert((pos.0 - 1, pos.1)); |
| 13 | + } |
| 14 | + if pos.0 < w - 1 && !garden.contains(&(pos.0 + 1, pos.1)) { |
| 15 | + new_positions.insert((pos.0 + 1, pos.1)); |
| 16 | + } |
| 17 | + if pos.1 > 0 && !garden.contains(&(pos.0, pos.1 - 1)) { |
| 18 | + new_positions.insert((pos.0, pos.1 - 1)); |
| 19 | + } |
| 20 | + if pos.1 < h - 1 && !garden.contains(&(pos.0, pos.1 + 1)) { |
| 21 | + new_positions.insert((pos.0, pos.1 + 1)); |
| 22 | + } |
| 23 | + } |
| 24 | + new_positions |
| 25 | +} |
| 26 | + |
| 27 | +fn main() { |
| 28 | + let mut garden: HashSet<(usize, usize)> = HashSet::new(); |
| 29 | + let mut positions: HashSet<(usize, usize)> = HashSet::new(); |
| 30 | + let mut garden_width: usize = 0; |
| 31 | + let mut garden_height: usize = 0; |
| 32 | + for (y, line) in std::io::stdin().lines().map(Result::unwrap).enumerate() { |
| 33 | + garden_height += 1; |
| 34 | + for (x, c) in line.chars().enumerate() { |
| 35 | + if garden_height == 1 { |
| 36 | + garden_width += 1; |
| 37 | + } |
| 38 | + if c == '#' { |
| 39 | + garden.insert((x, y)); |
| 40 | + } |
| 41 | + if c == 'S' { |
| 42 | + positions.insert((x, y)); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + for _ in 0..64 { |
| 47 | + positions = take_step(&garden, garden_width, garden_height, &positions); |
| 48 | + } |
| 49 | + println!("{}", positions.len()); |
| 50 | +} |
0 commit comments