Skip to content

Commit

Permalink
Day 3 - Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tudorpavel committed Dec 3, 2020
1 parent 4f947f9 commit 8a62c52
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
4 changes: 4 additions & 0 deletions day03/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ I assume Part 2 will ask us to find a slope which minimizes the number of trees
The implementation was mostly straightforward, for the HashMap of tree positions I found [HashSet](https://doc.rust-lang.org/std/collections/struct.HashSet.html) in Rust's standard library which is perfect since I actually wanted a Set to easily check presence of a given Point.

I thought about using a more functional style in `GeoMap#tree_count` but decided to go with this imperative approach. I was worried I couldn't easily generate a path Vector due to the wrapping of `x` by `self.width`.

## Part 2

The solution is simple enough, we just have to map `GeoMap#tree_count` over the given collection of Points and compute the product.
73 changes: 47 additions & 26 deletions day03/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct GeoMap {
}

impl GeoMap {
fn build(lines: Vec<String>) -> GeoMap {
fn build(lines: &[String]) -> GeoMap {
let mut trees = HashSet::new();

for (y, line) in lines.iter().enumerate() {
Expand All @@ -32,7 +32,7 @@ impl GeoMap {
}
}

fn tree_count(&self, slope: Point) -> usize {
fn tree_count(&self, slope: &Point) -> usize {
let mut current = slope.clone();
let mut count = 0;

Expand All @@ -49,42 +49,63 @@ impl GeoMap {
}
}

fn part1(lines: Vec<String>) -> usize {
fn part1(lines: &[String]) -> usize {
let geo_map = GeoMap::build(lines);
geo_map.tree_count(Point { x: 3, y: 1 })

geo_map.tree_count(&Point { x: 3, y: 1 })
}

fn part2(lines: &[String]) -> usize {
let geo_map = GeoMap::build(lines);

vec![
Point { x: 1, y: 1 },
Point { x: 3, y: 1 },
Point { x: 5, y: 1 },
Point { x: 7, y: 1 },
Point { x: 1, y: 2 },
]
.iter()
.map(|p| geo_map.tree_count(p))
.product()
}

fn main() {
let lines = utils::read_lines();
println!("Part 1: {}", part1(lines));
println!("Part 1: {}", part1(&lines));
println!("Part 1: {}", part2(&lines));
}

#[cfg(test)]
mod tests {
use super::*;

fn input() -> Vec<String> {
vec![
"..##.......",
"#...#...#..",
".#....#..#.",
"..#.#...#.#",
".#...##..#.",
"..#.##.....",
".#.#.#....#",
".#........#",
"#.##...#...",
"#...##....#",
".#..#...#.#",
]
.into_iter()
.map(|s| String::from(s))
.collect()
}

#[test]
fn part1_works() {
assert_eq!(
part1(
vec![
"..##.......",
"#...#...#..",
".#....#..#.",
"..#.#...#.#",
".#...##..#.",
"..#.##.....",
".#.#.#....#",
".#........#",
"#.##...#...",
"#...##....#",
".#..#...#.#",
]
.into_iter()
.map(|s| String::from(s))
.collect()
),
7
);
assert_eq!(part1(&input()), 7);
}

#[test]
fn part2_works() {
assert_eq!(part2(&input()), 336);
}
}

0 comments on commit 8a62c52

Please sign in to comment.