From b084f178eeb7390fd788b29978ed04db450f8596 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sat, 10 Dec 2022 17:39:13 +0100 Subject: [PATCH] Improved 2022 - Day 8 by stopping early if the highest possible score can not exceed the current highest score --- 2022/src/bin/day8.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/2022/src/bin/day8.rs b/2022/src/bin/day8.rs index e975e3e..d086724 100644 --- a/2022/src/bin/day8.rs +++ b/2022/src/bin/day8.rs @@ -125,6 +125,11 @@ impl aoc::Solver for Day { let mut distance_up = 0; let mut distance_down = 0; + // Bail early if the tree can not exceed the current highest + if x * (input[y].len()-x-1) * y * (input.len()-y-1) < score_highest { + continue; + } + for c in (0..x).rev() { distance_left += 1; if input[y][c] >= height { @@ -132,6 +137,10 @@ impl aoc::Solver for Day { } } + if distance_left * (input[y].len()-x-1) * y * (input.len()-y-1) < score_highest { + continue; + } + for c in x+1..input[y].len() { distance_right += 1; if input[y][c] >= height { @@ -139,6 +148,10 @@ impl aoc::Solver for Day { } } + if distance_left * distance_right * y * (input.len()-y-1) < score_highest { + continue; + } + for r in (0..y).rev() { distance_up += 1; if input[r][x] >= height { @@ -146,6 +159,10 @@ impl aoc::Solver for Day { } } + if distance_left * distance_right * distance_up * (input.len()-y-1) < score_highest { + continue; + } + for r in y+1..input[y].len() { distance_down += 1; if input[r][x] >= height {