156 lines
3.0 KiB
JavaScript
156 lines
3.0 KiB
JavaScript
const fs = require('fs');
|
|
|
|
//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
|
|
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
|
|
|
const treeMap = [];
|
|
|
|
for (i in inputArray) {
|
|
const trees = inputArray[i].split("");
|
|
treeMap.push(trees);
|
|
}
|
|
|
|
const width = treeMap[0].length;
|
|
const height = treeMap.length;
|
|
|
|
// Part One
|
|
|
|
|
|
let talls = width * height;
|
|
|
|
for (h = 1; h < height - 1; h++) {
|
|
for (w = 1; w < width - 1; w++) {
|
|
let short = true;
|
|
const myHeight = treeMap[h][w];
|
|
|
|
// look up
|
|
let heights = [];
|
|
for (y = h - 1; y >= 0; y--) {
|
|
heights.push(treeMap[y][w]);
|
|
}
|
|
let shorties = heights.filter((t) => t < myHeight);
|
|
if (heights.length === shorties.length) {
|
|
short = false;
|
|
}
|
|
// look down
|
|
heights = [];
|
|
for (y = h + 1; y < height; y++) {
|
|
heights.push(treeMap[y][w]);
|
|
}
|
|
shorties = heights.filter((t) => t < myHeight);
|
|
if (heights.length === shorties.length) {
|
|
short = false;
|
|
}
|
|
// look left
|
|
heights = [];
|
|
for (x = w - 1; x >= 0; x--) {
|
|
heights.push(treeMap[h][x]);
|
|
}
|
|
shorties = heights.filter((t) => t < myHeight);
|
|
if (heights.length === shorties.length) {
|
|
short = false;
|
|
}
|
|
// look right
|
|
heights = [];
|
|
for (x = w + 1; x < width; x++) {
|
|
heights.push(treeMap[h][x]);
|
|
}
|
|
shorties = heights.filter((t) => t < myHeight);
|
|
if (heights.length === shorties.length) {
|
|
short = false;
|
|
}
|
|
|
|
if (short) {
|
|
talls--;
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(talls);
|
|
|
|
|
|
// Part Two
|
|
|
|
const scores = [];
|
|
|
|
for (h = 1; h < height - 1; h++) {
|
|
for (w = 1; w < width - 1; w++) {
|
|
const myHeight = treeMap[h][w];
|
|
let myScore = 1;
|
|
|
|
// look up
|
|
let heights = [];
|
|
for (y = h - 1; y >= 0; y--) {
|
|
heights.push(treeMap[y][w]);
|
|
}
|
|
let visible = 0;
|
|
for (i in heights) {
|
|
if (heights[i] < myHeight) {
|
|
visible++
|
|
} else {
|
|
visible++
|
|
break;
|
|
}
|
|
}
|
|
myScore = myScore * visible;
|
|
|
|
// look down
|
|
heights = [];
|
|
for (y = h + 1; y < height; y++) {
|
|
heights.push(treeMap[y][w]);
|
|
}
|
|
visible = 0;
|
|
for (i in heights) {
|
|
if (heights[i] < myHeight) {
|
|
visible++
|
|
} else {
|
|
visible++
|
|
break;
|
|
}
|
|
}
|
|
myScore = myScore * visible;
|
|
|
|
// look left
|
|
heights = [];
|
|
for (x = w - 1; x >= 0; x--) {
|
|
heights.push(treeMap[h][x]);
|
|
}
|
|
visible = 0;
|
|
for (i in heights) {
|
|
if (heights[i] < myHeight) {
|
|
visible++
|
|
} else {
|
|
visible++
|
|
break;
|
|
}
|
|
}
|
|
myScore = myScore * visible;
|
|
|
|
// look right
|
|
heights = [];
|
|
for (x = w + 1; x < width; x++) {
|
|
heights.push(treeMap[h][x]);
|
|
}
|
|
visible = 0;
|
|
for (i in heights) {
|
|
if (heights[i] < myHeight) {
|
|
visible++
|
|
} else {
|
|
visible++
|
|
break;
|
|
}
|
|
}
|
|
myScore = myScore * visible;
|
|
|
|
scores.push(myScore);
|
|
}
|
|
}
|
|
|
|
scores.sort(function(a, b) {
|
|
return b - a;
|
|
});
|
|
|
|
console.log(scores[0]);
|
|
|
|
// functions
|