advent-of-code-2022/day08/index.js

71 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2022-12-08 21:59:48 +00:00
import { readFileSync } from "fs";
const input = readFileSync("trees.txt", "utf-8");
const arr = [];
2022-12-08 22:08:37 +00:00
let howManyVisible = 0, width, height = 0, highestScenicScore = 0;
2022-12-08 21:59:48 +00:00
input.split("\n").forEach(line => {
width = line.length;
arr.push(line.split(''));
height++;
});
let a = "";
for(let row = 0; row < height; row++) {
for(let column = 0; column < width; column++) {
if(row === 0 || column === 0 || row === height - 1 || column === width - 1) {
howManyVisible++;
continue;
}
const checkFor = arr[row][column];
let hiddenNorth = false, hiddenSouth = false, hiddenWest = false, hiddenEast = false;
2022-12-08 22:08:37 +00:00
let howManyNorth = 0, howManySouth = 0, howManyWest = 0, howManyEast = 0;
2022-12-08 21:59:48 +00:00
for(let i = row - 1; i >= 0; i--) {
2022-12-08 22:08:37 +00:00
howManyNorth++;
2022-12-08 21:59:48 +00:00
if(checkFor <= arr[i][column] && row !== i) {
hiddenNorth = true;
break;
}
}
for(let i = column - 1; i >= 0; i--) {
2022-12-08 22:08:37 +00:00
howManyWest++;
2022-12-08 21:59:48 +00:00
if(checkFor <= arr[row][i] && column !== i) {
hiddenWest = true;
break;
}
}
for(let i = row + 1; i < height; i++) {
2022-12-08 22:08:37 +00:00
howManySouth++;
2022-12-08 21:59:48 +00:00
if(checkFor <= arr[i][column] && row !== i) {
hiddenSouth = true;
break;
}
}
for(let i = column + 1; i < width; i++) {
2022-12-08 22:08:37 +00:00
howManyEast++;
2022-12-08 21:59:48 +00:00
if(checkFor <= arr[row][i] && column !== i) {
hiddenEast = true;
break;
}
}
if(!(
hiddenNorth &&
hiddenSouth &&
hiddenWest &&
hiddenEast
)) howManyVisible++;
2022-12-08 22:08:37 +00:00
const checkForHighestScore = howManyNorth * howManySouth * howManyWest * howManyEast;
if(checkForHighestScore > highestScenicScore) highestScenicScore = checkForHighestScore;
2022-12-08 21:59:48 +00:00
}
}
2022-12-08 22:08:37 +00:00
console.log(howManyVisible, highestScenicScore)