day 08 part 2.

This commit is contained in:
Gabe Venberg 2023-11-18 20:43:42 -06:00
parent 20b3e5e07b
commit 1469c3a32b
3 changed files with 114 additions and 4 deletions

View file

@ -1,7 +1,5 @@
#![allow(unused)]
use crate::utilities::*;
type StructuredInput = Vec<Vec<u8>>;
use crate::{utilities::*, StructuredInput};
pub fn part1(input: &StructuredInput) -> usize {
let mut acc: usize = 0;

110
src/day08/part2.rs Normal file
View file

@ -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);
}
}

View file

@ -3,6 +3,8 @@ mod part1;
mod part2;
mod utilities;
type StructuredInput = Vec<Vec<u8>>;
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));
}