From 54c81ba1ca6c9ac64200b759ba09e473ae4dada4 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Thu, 8 Dec 2022 20:29:02 -0500 Subject: [PATCH] part 2 done; just did it the hard way --- 08/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/08/main.rs b/08/main.rs index 9eb5fb4..d9afdcb 100644 --- a/08/main.rs +++ b/08/main.rs @@ -5,8 +5,8 @@ use std::io::BufRead; use std::path::Path; use std::time::Instant; -#[derive(Debug, Clone)] -struct Tree(i8, bool); +#[derive(Debug, Clone, Copy)] +struct Tree(i8, bool); // height, visible from outside fn main() { let now = Instant::now(); @@ -85,4 +85,63 @@ fn part_one() { println!("{}", total); } -fn part_two() {} +fn part_two() { + let mut grid: Vec> = 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); +}