-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.js
41 lines (33 loc) · 1.09 KB
/
part2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { getInputString } = require("../utils/getInputString.js");
const file = getInputString(8, false);
const transpose = (array) => {
return array[0].map((_, colIndex) => array.map(row => row[colIndex]));
}
const trees = file.split('\n').map(row => row.split(""));
const transposedTrees = transpose(trees);
const getViewScoreInRow = (treeIndex, row) => {
// use some method to break when a tall tree is in the way
let leftScore = 0;
row.slice(0, treeIndex).reverse().some(tree => {
leftScore++;
return tree >= row[treeIndex];
});
let rightScore = 0;
row.slice(treeIndex + 1, row.length).some(tree => {
rightScore++;
return tree >= row[treeIndex];
});
return leftScore * rightScore
}
let biggestScore = 0;
for(let y = 0; y <= trees.length - 1; y++) {
for(let x = 0; x <= trees[y].length -1; x++) {
const rowScore = getViewScoreInRow(x, trees[y])
const columnScore = getViewScoreInRow(y, transposedTrees[x])
const treeScore = rowScore * columnScore;
if(treeScore > biggestScore) {
biggestScore = treeScore
}
}
}
console.log(biggestScore);