-
-
Save Aehmlo/ad193d056bc27847166a34f8aea50cbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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