summaryrefslogtreecommitdiff
path: root/aoc22
diff options
context:
space:
mode:
authormathiasmagnusson <mathias@magnusson.space>2022-12-12 19:46:20 +0100
committermathiasmagnusson <mathias@magnusson.space>2022-12-12 19:46:20 +0100
commit616bec7e8b07e6d7c669b2b9142da14feebe8885 (patch)
tree2a37ab4ff7424d701b0d5f008eec9e394981d719 /aoc22
parentb7e2fe6160280472a625a8867ea5177b0155737c (diff)
downloadprogramming-problem-solving-616bec7e8b07e6d7c669b2b9142da14feebe8885.tar.gz
Day 8 part 2
Diffstat (limited to 'aoc22')
-rw-r--r--aoc22/day8/src/main.rs39
1 files changed, 38 insertions, 1 deletions
diff --git a/aoc22/day8/src/main.rs b/aoc22/day8/src/main.rs
index f73543d..66b2322 100644
--- a/aoc22/day8/src/main.rs
+++ b/aoc22/day8/src/main.rs
@@ -10,7 +10,7 @@ fn part1(input: &str) {
let mut ans = 0;
for (y, line) in input.iter().enumerate() {
- for (x, c) in line.iter().enumerate() {
+ for (x, _) in line.iter().enumerate() {
if visible(&input, x, y) {
ans += 1;
}
@@ -20,6 +20,43 @@ fn part1(input: &str) {
}
fn part2(input: &str) {
+ let input = input.split_whitespace().map(|x| x.bytes().collect::<Vec<_>>()).collect::<Vec<_>>();
+
+ let mut ans = 0;
+ for (y, line) in input.iter().enumerate() {
+ for (x, _) in line.iter().enumerate() {
+ ans = ans.max(scenic_score(&input, x, y));
+ }
+ }
+ println!("{}", ans);
+}
+
+fn scenic_score(heights: &[Vec<u8>], x: usize, y: usize) -> u32 {
+ let mut score = 1;
+ for (dy, dx) in &[(0, 1), (0, -1), (1, 0), (-1, 0)] {
+ let height = heights[y][x];
+
+ let mut seen = 0;
+
+ let mut sx = x as i32;
+ let mut sy = y as i32;
+ loop {
+ sx += dx;
+ sy += dy;
+
+ if sx < 0 || sy < 0 || sy >= heights.len() as i32 || sx >= heights[sy as usize].len() as i32 {
+ break;
+ }
+
+ seen += 1;
+
+ if heights[sy as usize][sx as usize] >= height {
+ break;
+ }
+ }
+ score *= seen;
+ }
+ score
}
fn visible(heights: &[Vec<u8>], x: usize, y: usize) -> bool {