Improved 2022 - Day 8 by stopping early if the highest possible score can not exceed the current highest score

This commit is contained in:
Dreaded_X 2022-12-10 17:39:13 +01:00
parent 225537f364
commit b084f178ee

View File

@ -125,6 +125,11 @@ impl aoc::Solver for Day {
let mut distance_up = 0; let mut distance_up = 0;
let mut distance_down = 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() { for c in (0..x).rev() {
distance_left += 1; distance_left += 1;
if input[y][c] >= height { 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() { for c in x+1..input[y].len() {
distance_right += 1; distance_right += 1;
if input[y][c] >= height { 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() { for r in (0..y).rev() {
distance_up += 1; distance_up += 1;
if input[r][x] >= height { 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() { for r in y+1..input[y].len() {
distance_down += 1; distance_down += 1;
if input[r][x] >= height { if input[r][x] >= height {