diff --git a/src/day08/part1.rs b/src/day08/part1.rs index 77b2a34..9637611 100644 --- a/src/day08/part1.rs +++ b/src/day08/part1.rs @@ -1,7 +1,5 @@ #![allow(unused)] -use crate::utilities::*; - -type StructuredInput = Vec>; +use crate::{utilities::*, StructuredInput}; pub fn part1(input: &StructuredInput) -> usize { let mut acc: usize = 0; diff --git a/src/day08/part2.rs b/src/day08/part2.rs new file mode 100644 index 0000000..a018a2d --- /dev/null +++ b/src/day08/part2.rs @@ -0,0 +1,110 @@ +#![allow(unused)] +use crate::{utilities::*, StructuredInput}; + +pub fn part2(input: &StructuredInput) -> usize { + let mut max: usize = 0; + for y in 0..input.len() { + for x in 0..input[0].len() { + let tree_score = tree_score(x, y, input); + if max < tree_score{ + max = tree_score; + println!("found new max with score {} at {}, {}", tree_score, x, y) + } + } + } + max +} +fn tree_score(x: usize, y: usize, input: &StructuredInput) -> usize { + trees_visible_north(x, y, input) + * trees_visible_east(x, y, input) + * trees_visible_south(x, y, input) + * trees_visible_west(x, y, input) +} +fn trees_visible_north(x: usize, y: usize, input: &StructuredInput) -> usize { + if y == 0 { + return 0; + }; + let hight = input[y][x]; + let mut trees: usize = 0; + for y in (0..y).rev() { + trees += 1; + if input[y][x] >= hight { + break + } + } + trees +} +fn trees_visible_east(x: usize, y: usize, input: &StructuredInput) -> usize { + if x == input[0].len() { + return 0; + } + let hight = input[y][x]; + let mut trees: usize = 0; + for x in (x + 1)..input[0].len() { + trees += 1; + if input[y][x] >= hight { + break + } + } + trees +} +fn trees_visible_south(x: usize, y: usize, input: &StructuredInput) -> usize { + if y == input.len() { + return 0; + }; + let hight = input[y][x]; + let mut trees: usize = 0; + for y in (y + 1)..input.len() { + trees += 1; + if input[y][x] >= hight { + break + } + } + trees +} +fn trees_visible_west(x: usize, y: usize, input: &StructuredInput) -> usize { + if x == input[0].len() { + return 0; + } + let hight = input[y][x]; + let mut trees: usize = 0; + for x in (0..x).rev() { + trees += 1; + if input[y][x] >= hight { + break + } + } + trees +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part2() { + let input = vec![ + vec![3, 0, 3, 7, 3], + vec![2, 5, 5, 1, 2], + vec![6, 5, 3, 3, 2], + vec![3, 3, 5, 4, 9], + vec![3, 5, 3, 9, 0], + ]; + assert_eq!(part2(&input), 8); + } + #[test] + fn test_trees_visible() { + let input = vec![ + vec![3, 0, 3, 7, 3], + vec![2, 5, 5, 1, 2], + vec![6, 5, 3, 3, 2], + vec![3, 3, 5, 4, 9], + vec![3, 5, 3, 9, 0], + ]; + assert_eq!(trees_visible_north(2, 3, &input), 2); + assert_eq!(trees_visible_east(2, 3, &input), 2); + assert_eq!(trees_visible_south(2, 3, &input), 1); + assert_eq!(trees_visible_west(2, 3, &input), 2); + assert_eq!(tree_score(2, 3, &input), 8); + } +} diff --git a/src/day08/solve.rs b/src/day08/solve.rs index f1896c1..9ce7247 100644 --- a/src/day08/solve.rs +++ b/src/day08/solve.rs @@ -3,6 +3,8 @@ mod part1; mod part2; mod utilities; +type StructuredInput = Vec>; + fn main() { let input = include_str!("./input.txt"); let structured_input = utilities::parse(input); @@ -11,5 +13,5 @@ fn main() { println!("Result: {}", part1::part1(&structured_input)); println!("Part Two"); - println!("Result: {}", part2::part2()); + println!("Result: {}", part2::part2(&structured_input)); }