From 69b0f79024f63591c00d49719aae82fdd538f0f9 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Thu, 8 Dec 2022 23:08:37 +0100 Subject: [PATCH] 08_2 --- day08/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/day08/index.js b/day08/index.js index 8c8777e..01fa629 100644 --- a/day08/index.js +++ b/day08/index.js @@ -4,7 +4,7 @@ const input = readFileSync("trees.txt", "utf-8"); const arr = []; -let howManyVisible = 0, width, height = 0; +let howManyVisible = 0, width, height = 0, highestScenicScore = 0; input.split("\n").forEach(line => { width = line.length; @@ -22,8 +22,10 @@ for(let row = 0; row < height; row++) { } const checkFor = arr[row][column]; let hiddenNorth = false, hiddenSouth = false, hiddenWest = false, hiddenEast = false; + let howManyNorth = 0, howManySouth = 0, howManyWest = 0, howManyEast = 0; for(let i = row - 1; i >= 0; i--) { + howManyNorth++; if(checkFor <= arr[i][column] && row !== i) { hiddenNorth = true; break; @@ -31,6 +33,7 @@ for(let row = 0; row < height; row++) { } for(let i = column - 1; i >= 0; i--) { + howManyWest++; if(checkFor <= arr[row][i] && column !== i) { hiddenWest = true; break; @@ -38,6 +41,7 @@ for(let row = 0; row < height; row++) { } for(let i = row + 1; i < height; i++) { + howManySouth++; if(checkFor <= arr[i][column] && row !== i) { hiddenSouth = true; break; @@ -45,6 +49,7 @@ for(let row = 0; row < height; row++) { } for(let i = column + 1; i < width; i++) { + howManyEast++; if(checkFor <= arr[row][i] && column !== i) { hiddenEast = true; break; @@ -57,7 +62,10 @@ for(let row = 0; row < height; row++) { hiddenWest && hiddenEast )) howManyVisible++; + + const checkForHighestScore = howManyNorth * howManySouth * howManyWest * howManyEast; + if(checkForHighestScore > highestScenicScore) highestScenicScore = checkForHighestScore; } } -console.log(howManyVisible) \ No newline at end of file +console.log(howManyVisible, highestScenicScore) \ No newline at end of file