part 2 done; just did it the hard way

This commit is contained in:
David 2022-12-08 20:29:02 -05:00
parent ae55b29ff4
commit 54c81ba1ca
1 changed files with 62 additions and 3 deletions

View File

@ -5,8 +5,8 @@ use std::io::BufRead;
use std::path::Path; use std::path::Path;
use std::time::Instant; use std::time::Instant;
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
struct Tree(i8, bool); struct Tree(i8, bool); // height, visible from outside
fn main() { fn main() {
let now = Instant::now(); let now = Instant::now();
@ -85,4 +85,63 @@ fn part_one() {
println!("{}", total); println!("{}", total);
} }
fn part_two() {} fn part_two() {
let mut grid: Vec<Vec<Tree>> = vec![];
let mut best = 0;
if let Ok(lines) = read_lines("./inputs/input") {
for (i, line) in lines.enumerate() {
if let Ok(row) = line {
grid.push(vec![]);
for c in row.chars() {
grid[i].push(Tree((c as i8) - 48, false));
}
}
}
}
// alright, we'll do this the hard way
// we can skip the edges, because they all multiply by 0 in one way or another
for i in 1..grid.len() - 1 {
for j in 1..grid.len() - 1 {
let (mut score, mut acc) = (1, 0); // a base score and the counter we'll reuse
for x in (0..j).rev() {
// left
acc += 1;
if grid[i][x].0 >= grid[i][j].0 {
break;
}
}
score *= acc;
acc = 0;
for x in (0..i).rev() {
// up
acc += 1;
if grid[x][j].0 >= grid[i][j].0 {
break;
}
}
score *= acc;
acc = 0;
for x in (j + 1)..grid.len() {
// right
acc += 1;
if grid[i][x].0 >= grid[i][j].0 {
break;
}
}
score *= acc;
acc = 0;
for x in (i + 1)..grid.len() {
// down
acc += 1;
if grid[x][j].0 >= grid[i][j].0 {
break;
}
}
score *= acc;
if score > best {
best = score;
}
}
}
println!("{}", best);
}