generated from mariotacke/template-advent-of-code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
part1.js
29 lines (23 loc) · 732 Bytes
/
part1.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
module.exports = (input) => {
const heightMap = input
.split('\n')
.map((line) => line.trim().split('').map((point) => +point));
const width = heightMap[0].length;
const height = heightMap.length;
const isLowest = (x, y) => {
const point = heightMap[y][x];
return point < (x - 1 < 0 ? 9 : heightMap[y][x - 1]) &&
point < (x + 1 >= width ? 9 : heightMap[y][x + 1]) &&
point < (y - 1 < 0 ? 9 : heightMap[y - 1][x]) &&
point < (y + 1 >= height ? 9 : heightMap[y + 1][x]);
};
let total = 0;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
if (isLowest(x, y)) {
total += heightMap[y][x] + 1;
}
}
}
return total;
};