-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.js
executable file
·57 lines (55 loc) · 1.9 KB
/
day08.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
#!/usr/bin/env node
// https://adventofcode.com/2022/day/8
const input = `30373
25512
65332
33549
35390`;
// Part 1 - Count visible trees
const trees = input
.split("\n")
.map(line => line.split(""))
.map(line => line.map(tree => Number(tree)));
let sum = 0;
for (let y = 0; y < trees.length; y++) {
for (let x = 0; x < trees.length; x++) {
const tree = trees[y][x];
const row = trees[y];
const col = trees.flatMap(row => row[x]);
const leftTrees = row.slice(0, x);
const rightTrees = row.slice(x + 1);
const topTrees = col.slice(0, y);
const bottomTrees = col.slice(y + 1);
const visible = [leftTrees, rightTrees, topTrees, bottomTrees]
.map(list => list.filter(other => tree <= other))
.map(list => list.length === 0)
.reduce((x, y) => x || y);
visible && sum++;
}
}
console.log(sum);
// Part 2 - Find tree with most visible neighbors
function countVisible(trees, height) {
if (!trees.length) {
return 0;
}
let firstBlocker = trees.findIndex(blocker => height <= blocker);
return -1 !== firstBlocker ? firstBlocker + 1 : trees.length;
}
let mostVisible = 0;
for (let y = 0; y < trees.length; y++) {
for (let x = 0; x < trees.length; x++) {
const tree = trees[y][x];
const row = trees[y];
const col = trees.flatMap(row => row[x]);
const leftTrees = row.slice(0, x).reverse(); // closest trees first
const rightTrees = row.slice(x + 1);
const topTrees = col.slice(0, y).reverse(); // closest trees first
const bottomTrees = col.slice(y + 1);
const visible = [leftTrees, rightTrees, topTrees, bottomTrees]
.map(list => countVisible(list, tree))
.reduce((x, y) => x * y); // as defined, edge trees will score 0
mostVisible = Math.max(visible, mostVisible);
}
}
console.log(mostVisible);