From b42678d3566b0c24866efb467ba9f8f4daba1cb9 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 9 Dec 2022 01:12:54 +0100 Subject: [PATCH] Made solution less complex and increased performance --- 2022/src/bin/day8.rs | 69 ++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/2022/src/bin/day8.rs b/2022/src/bin/day8.rs index 5e5982f..b04e9da 100644 --- a/2022/src/bin/day8.rs +++ b/2022/src/bin/day8.rs @@ -45,37 +45,25 @@ mod tests { } } // -- Helpers -- -fn parse(input: &str) -> (usize, Vec>) { - let size = input.lines().count(); - let input = input +fn parse(input: &str) -> Vec> { + input .lines() - .map(|line| line.chars().filter_map(|c| c.to_digit(10)).collect::>()) - .collect::>(); - - (size, input) + .map(|line| line.chars().filter_map(|c| c.to_digit(10)).map(|d| d as i32).collect::>()) + .collect::>() } -fn is_visible(size: usize, highest: &mut u32, height: u32, y: usize, x: usize) -> bool { - match (y, x, height) { - (r, _, _) if r == 0 || r == size-1 => true, - (_, c, _) if c == 0 || c == size-1 => { - *highest = height; - true - }, - (_, _, h) => { - if h > *highest { - *highest = h; - true - } else { - false - } - } +fn is_visible(highest: &mut i32, height: i32) -> Option { + if height > *highest { + *highest = height; + Some(true) + } else { + Some(false) } } // Consume the vector and perform the transpose by swapping around elements -fn transpose(size: usize, mut input: Vec>) -> Vec> { - for y in 0..size { +fn transpose( mut input: Vec>) -> Vec> { + for y in 0..input.len() { for x in 0..y { unsafe { let pa: *mut T = &mut input[x][y]; @@ -89,27 +77,20 @@ fn transpose(size: usize, mut input: Vec>) -> Vec input } -fn process_1d(input: &Vec>) -> Vec> { +fn process_1d(input: &Vec>) -> Vec> { input.iter() - .enumerate() - .map(|(y, row)| { - let size = row.len(); - let left = row.iter() - .enumerate() - .scan(0, |mut highest, (x, &height)| Some(is_visible(size, &mut highest, height, y, x))); - + .map(|row| { let mut right = row.iter() - .enumerate() .rev() - .scan(0, |mut highest, (x, &height)| Some(is_visible(size, &mut highest, height, y, x))) + .scan(-1, |mut highest, &height| is_visible(&mut highest, height)) .collect::>(); - right.reverse(); - left.zip(right.iter()) + row.iter() + .scan(-1, |mut highest, &height| is_visible(&mut highest, height)) + .zip(right.iter()) .map(|(left, &right)| left || right) .collect::>() - }).collect() } @@ -122,20 +103,20 @@ impl aoc::Solver for Day { } fn part1(input: &str) -> Self::Output { - let (size, input) = parse(input); + let input = parse(input); let horizontal = process_1d(&input); - let vertical = transpose(size, process_1d(&transpose(size, input))); + let vertical = transpose(process_1d(&transpose(input))); horizontal.iter().flatten().zip(vertical.iter().flatten()).filter(|(&horizontal, &vertical)| horizontal || vertical).count() } fn part2(input: &str) -> Self::Output { - let (size, input) = parse(input); + let input = parse(input); let mut score_highest = 0; - for y in 0..size { - for x in 0..size { + for y in 0..input.len() { + for x in 0..input[y].len() { let height = input[y][x]; let mut distance_left = 0; @@ -150,7 +131,7 @@ impl aoc::Solver for Day { } } - for c in x+1..size { + for c in x+1..input[y].len() { distance_right += 1; if input[y][c] >= height { break; @@ -164,7 +145,7 @@ impl aoc::Solver for Day { } } - for r in y+1..size { + for r in y+1..input[y].len() { distance_down += 1; if input[r][x] >= height { break;