Skip to content

Instantly share code, notes, and snippets.

@Aehmlo

Aehmlo/day3.rs Secret

Last active December 3, 2020 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aehmlo/ad193d056bc27847166a34f8aea50cbe to your computer and use it in GitHub Desktop.
Save Aehmlo/ad193d056bc27847166a34f8aea50cbe to your computer and use it in GitHub Desktop.
static INPUT: &str = include_str!("../inputs/3.txt");
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct Slope {
// positive x is right
pub x: u8,
// positive y is down
pub y: u8,
}
impl Default for Slope {
fn default() -> Self {
Self { x: 1, y: 1 }
}
}
impl From<(u8, u8)> for Slope {
fn from((x, y): (u8, u8)) -> Self {
Self { x, y }
}
}
impl From<u8> for Slope {
fn from(x: u8) -> Self {
Self {
x,
..Default::default()
}
}
}
fn count_trees(slope: impl Into<Slope>) -> usize {
let slope = slope.into();
INPUT
.lines()
.step_by(slope.y as usize)
.enumerate()
.filter(|(d, s)| s.chars().cycle().nth(d * slope.x as usize) == Some('#'))
.count()
}
fn main() {
println!("{}", count_trees(3));
let part_two = [(1, 1), (3, 1), (5, 1), (7, 1), (1_u8, 2_u8)]
.iter()
.copied()
.map(count_trees)
.product::<usize>();
println!("{}", part_two);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment