-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.js
131 lines (96 loc) Β· 3.2 KB
/
08.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* eslint-disable unicorn/prevent-abbreviations */
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {readInput} from '../../../util.js';
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
const input = await readInput(directoryPath);
const treeGrid = input
.split('\n')
.filter(line => line !== '')
.map(line => [...line]);
// Set initial to all trees at the edge
let visibleTrees = treeGrid.length * 2 + (treeGrid[0].length * 2 - 4);
function isVisibleOnLeftSide(treeHeight, xPosition, yPosition) {
for (let x = xPosition - 1; x >= 0; x--) {
if (treeGrid[yPosition][x] >= treeHeight) return false;
}
return true;
}
function isVisibleOnRightSide(treeHeight, xPosition, yPosition) {
for (let x = xPosition + 1; x < treeGrid[yPosition].length; x++) {
if (treeGrid[yPosition][x] >= treeHeight) return false;
}
return true;
}
function isVisibleOnTop(treeHeight, xPosition, yPosition) {
for (let y = yPosition - 1; y >= 0; y--) {
if (treeGrid[y][xPosition] >= treeHeight) return false;
}
return true;
}
function isVisibleOnBottom(treeHeight, xPosition, yPosition) {
for (let y = yPosition + 1; y < treeGrid.length; y++) {
if (treeGrid[y][xPosition] >= treeHeight) return false;
}
return true;
}
for (let i = 1; i < treeGrid.length - 1; i++) {
for (let j = 1; j < treeGrid[i].length - 1; j++) {
const currentTree = Number(treeGrid[i][j]);
const isCurrentTreeVisible =
isVisibleOnLeftSide(currentTree, j, i) ||
isVisibleOnRightSide(currentTree, j, i) ||
isVisibleOnTop(currentTree, j, i) ||
isVisibleOnBottom(currentTree, j, i);
if (isCurrentTreeVisible) visibleTrees++;
}
}
console.log('Visible trees:', visibleTrees);
function findLeftScenicScore(treeHeight, xPosition, yPosition) {
let viewableTrees = 0;
for (let x = xPosition - 1; x >= 0; x--) {
viewableTrees++;
if (treeGrid[yPosition][x] >= treeHeight) break;
}
return viewableTrees;
}
function findRightScenicScore(treeHeight, xPosition, yPosition) {
let viewableTrees = 0;
for (let x = xPosition + 1; x < treeGrid[yPosition].length; x++) {
viewableTrees++;
if (treeGrid[yPosition][x] >= treeHeight) break;
}
return viewableTrees;
}
function findTopScenicScore(treeHeight, xPosition, yPosition) {
let viewableTrees = 0;
for (let y = yPosition - 1; y >= 0; y--) {
viewableTrees++;
if (treeGrid[y][xPosition] >= treeHeight) break;
}
return viewableTrees;
}
function findBottomScenicScore(treeHeight, xPosition, yPosition) {
let viewableTrees = 0;
for (let y = yPosition + 1; y < treeGrid.length; y++) {
viewableTrees++;
if (treeGrid[y][xPosition] >= treeHeight) break;
}
return viewableTrees;
}
const scenicScores = [];
for (let i = 1; i < treeGrid.length - 1; i++) {
for (let j = 1; j < treeGrid[i].length - 1; j++) {
const currentTree = Number(treeGrid[i][j]);
const scenicScore =
findLeftScenicScore(currentTree, j, i) *
findRightScenicScore(currentTree, j, i) *
findTopScenicScore(currentTree, j, i) *
findBottomScenicScore(currentTree, j, i);
scenicScores.push({currentTree, scenicScore});
}
}
const sortedScenicScores = scenicScores.sort(
(a, b) => b.scenicScore - a.scenicScore,
);
console.log('Highest scenic score:', sortedScenicScores[0]);