/* Starting at the top-left corner of your map and following a slope of right 3 and down 1, how many trees would you encounter? */ use crate::Problem; pub struct Problem3 { map: Vec>, } impl Problem3 { fn get_tile(&self, x: i32, y: i32) -> i32 { let dim_y = self.map.len() as i32; let dim_x = self.map.get(0).unwrap().len() as i32; let ind_x = (x % dim_x) as usize; let ind_y = (y % dim_y) as usize; *self.map.get(ind_y).unwrap().get(ind_x).unwrap() } fn traverse(&self, dx: i32, dy: i32) -> i64 { let mut x = 0; let mut count = 0; for y in ((dy as usize)..self.map.len()).step_by(dy as usize) { x += dx; if self.get_tile(x as i32, y as i32) == 1 { count += 1; // println!("tree at {}, {}", x, y); } } count as i64 } } impl Problem for Problem3 { fn new(input: &String) -> Self { Problem3 { map: input .split("\n") .filter_map(|s| { let s = s.trim(); if !s.is_empty() { let mut accum = Vec::new(); for i in s.chars() { if i == '#' { accum.push(1); } else { accum.push(0); } } Some(accum) } else { None } }).collect(), } } fn run_part1(&self) { let v : i64 = self.traverse(1, 1); println!("{}", v); } fn run_part2(&self) { let v : i64 = self.traverse(1, 1) * self.traverse(3, 1) * self.traverse(5, 1) * self.traverse(7, 1) * self.traverse(1, 2); println!("{}", v); } }