-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ts
37 lines (32 loc) · 1.01 KB
/
part2.ts
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
import * as fs from 'fs';
import { getNeighbors, getNeighborPositions } from '../../utils';
const input = fs
.readFileSync('input', 'utf8')
.split('\n')
.map((x) => x.split('').map(Number));
const getAllNeighborPositions = (positionSet: Set<string>, x: number, y: number) => {
const positionKey = `${x},${y}`;
if (input[x][y] === 9 || positionSet.has(positionKey)) {
return;
}
positionSet.add(positionKey);
getNeighborPositions(input, x, y).map((p) => getAllNeighborPositions(positionSet, p.x, p.y));
return positionSet;
};
let basins = [];
for (let x = 0; x < input.length; x++) {
for (let y = 0; y < input[x].length; y++) {
const neighbors = getNeighbors(input, x, y);
const highNeighbors = neighbors.filter((n) => n > input[x][y]);
if (highNeighbors.length === neighbors.length) {
const positionSet: Set<string> = new Set();
basins.push(getAllNeighborPositions(positionSet, x, y).size);
}
}
}
console.log(
basins
.sort((a, b) => b - a)
.slice(0, 3)
.reduce((prev, size) => prev * size, 1),
);