Skip to content

Instantly share code, notes, and snippets.

@erikaderstedt
Last active December 3, 2020 05:42
Show Gist options
  • Save erikaderstedt/f0af1a07caa8e8088a2cd4ef11c5184f to your computer and use it in GitHub Desktop.
Save erikaderstedt/f0af1a07caa8e8088a2cd4ef11c5184f to your computer and use it in GitHub Desktop.
use crate::common::Solution;
use crate::grid::{Grid,GridElement,Position};
#[derive(Eq,PartialEq,Clone,Debug)]
enum ForestTile {
Open,
Tree,
}
impl GridElement for ForestTile {
fn from_char(c: &char) -> Option<ForestTile> {
match c {
'.' => Some(ForestTile::Open),
'#' => Some(ForestTile::Tree),
_ => None,
}
}
}
pub fn solve(lines: &[String]) -> Solution {
let grid: Grid<ForestTile> = Grid::load(lines);
let slopes = [(1,1),(3,1),(5,1),(7,1),(1,2)];
let num_trees: Vec<usize> = slopes.iter().map(|(col_step,row_step)| -> usize {
(0..grid.rows)
.step_by(*row_step)
.enumerate()
.filter(|(col_index,row)| -> bool {
grid[&Position { row: *row, column: (col_index * col_step) % grid.cols }] == ForestTile::Tree
})
.count()
}).collect();
let p2: usize = num_trees.iter().product();
Solution { part_1: num_trees[1].to_string(), part_2: p2.to_string() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment