aoc2022 day08 part 2
This commit is contained in:
parent
4ff915e241
commit
f6ad00ad27
@ -1,8 +1,7 @@
|
|||||||
const COLUMNS: usize = 99;
|
const COLUMNS: usize = 99;
|
||||||
const ROWS: usize = 99;
|
const ROWS: usize = 99;
|
||||||
|
|
||||||
pub fn task_one(input: &str) -> String {
|
fn parse_into_grid(input: &str) -> [[u8; COLUMNS]; ROWS] {
|
||||||
let mut visible_trees = 0;
|
|
||||||
let mut grid: [[u8; COLUMNS]; ROWS] = [[0; COLUMNS]; ROWS];
|
let mut grid: [[u8; COLUMNS]; ROWS] = [[0; COLUMNS]; ROWS];
|
||||||
|
|
||||||
for (i, line) in input.trim().lines().enumerate() {
|
for (i, line) in input.trim().lines().enumerate() {
|
||||||
@ -13,6 +12,13 @@ pub fn task_one(input: &str) -> String {
|
|||||||
|
|
||||||
// println!("{:?}", grid);
|
// println!("{:?}", grid);
|
||||||
|
|
||||||
|
grid
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task_one(input: &str) -> String {
|
||||||
|
let mut visible_trees = 0;
|
||||||
|
let grid = parse_into_grid(&input);
|
||||||
|
|
||||||
for i in 0..ROWS {
|
for i in 0..ROWS {
|
||||||
for j in 0..COLUMNS {
|
for j in 0..COLUMNS {
|
||||||
let tree = grid[i][j];
|
let tree = grid[i][j];
|
||||||
@ -65,5 +71,63 @@ pub fn task_one(input: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn task_two(input: &str) -> String {
|
pub fn task_two(input: &str) -> String {
|
||||||
String::new()
|
let mut score = 0;
|
||||||
|
let grid = parse_into_grid(&input);
|
||||||
|
|
||||||
|
for i in 0..ROWS {
|
||||||
|
for j in 0..COLUMNS {
|
||||||
|
let tree = grid[i][j];
|
||||||
|
|
||||||
|
// check upwards
|
||||||
|
let mut visible_up = 0;
|
||||||
|
for k in (0..i).rev() {
|
||||||
|
visible_up += 1;
|
||||||
|
if grid[k][j] >= tree {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check to the right
|
||||||
|
let mut visible_right = 0;
|
||||||
|
for k in (j + 1)..COLUMNS {
|
||||||
|
visible_right += 1;
|
||||||
|
if grid[i][k] >= tree {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check downwards
|
||||||
|
let mut visible_down = 0;
|
||||||
|
for k in (i + 1)..ROWS {
|
||||||
|
visible_down += 1;
|
||||||
|
if grid[k][j] >= tree {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check to the left
|
||||||
|
let mut visible_left = 0;
|
||||||
|
for k in (0..j).rev() {
|
||||||
|
visible_left += 1;
|
||||||
|
if grid[i][k] >= tree {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_score = visible_up * visible_right * visible_down * visible_left;
|
||||||
|
if new_score > score {
|
||||||
|
score = new_score;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
score.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_example() {
|
||||||
|
// set ROWS and COLUMNS to 5 for the test to pass
|
||||||
|
let input = "30373\n25512\n65332\n33549\n35390\n";
|
||||||
|
|
||||||
|
assert_eq!(task_two(&input), 8.to_string());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user