diff --git a/2020/03/main.rs b/2020/03/main.rs index 1da4c4b..f8a3e10 100644 --- a/2020/03/main.rs +++ b/2020/03/main.rs @@ -2,8 +2,6 @@ use std::error::Error; use grid::{Grid, GridLine}; -use crate::grid::Cell; - #[path = "../utils/mod.rs"] mod utils; @@ -11,24 +9,22 @@ fn main() -> Result<(), Box<dyn Error>> { let lines = utils::parse_input_lines::<GridLine>()?; let grid = Grid::from(lines)?; - let mut x = 0; - let mut y = 0; - let mut trees = 0; + println!("Total trees on slope going 3 right 1 down: {}", grid.count_trees_on_slope(3, 1)); - loop { - match grid.get_at(x, y) { - None => break, - Some(Cell::Open) => {} - Some(Cell::Tree) => { - trees += 1; - } - } - - x += 3; - y += 1; + let mut product = 1u32; + let slopes = vec![ + (1, 1), + (3, 1), + (5, 1), + (7, 1), + (1, 2) + ]; + + for (down, right) in slopes { + product *= grid.count_trees_on_slope(down, right); } - println!("Total trees: {}", trees); + println!("Product of all attempted slopes: {}", product); Ok(()) } @@ -58,6 +54,27 @@ mod grid { let line = self.data.get(y as usize)?; Some(line.get_at(x)) } + + pub fn count_trees_on_slope(&self, step_x: i32, step_y: i32) -> u32 { + let mut x = 0; + let mut y = 0; + let mut trees = 0u32; + + loop { + match self.get_at(x, y) { + None => break, + Some(Cell::Open) => {} + Some(Cell::Tree) => { + trees += 1; + } + } + + x += step_x; + y += step_y; + } + + return trees; + } } pub struct GridLine {