-
Notifications
You must be signed in to change notification settings - Fork 0
/
09.ts
81 lines (70 loc) · 2.23 KB
/
09.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
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
import {loadData, product, sum} from '../shared/utils';
export type DataType = Array<number>;
/* istanbul ignore next */
export const parse = async (): Promise<Array<DataType>> =>
(await loadData(2021, 9)).split('\n').map(row => row.split('').map(Number));
const getNeighbors = (
data: Array<DataType>,
row: number,
col: number
): Array<number> => {
return [
data[row - 1]?.[col - 1],
data[row + 0]?.[col - 1],
data[row + 1]?.[col - 1],
data[row - 1]?.[col + 0],
data[row + 1]?.[col + 0],
data[row - 1]?.[col + 1],
data[row + 0]?.[col + 1],
data[row + 1]?.[col + 1],
].filter(n => n != null);
};
export const part1 = (data: Array<DataType>): number => {
const lowPoints: Array<number> = [];
for (let row = 0; row < data.length; row += 1) {
for (let col = 0; col < data[row].length; col += 1) {
const neighbors = getNeighbors(data, row, col);
if (neighbors.every(n => n > data[row][col])) {
lowPoints.push(data[row][col]);
}
}
}
return sum(lowPoints) + lowPoints.length;
};
export function getBasin(
data: Array<DataType>,
col: number,
row: number,
basin: Array<string>
): Array<string> {
if (data[row]?.[col] == null) {
return [];
}
if (basin.includes([row, col].join(','))) {
return [];
}
if (data[row][col] === 9) {
return [];
}
let newBasin = [...basin, [row, col].join(',')];
newBasin = newBasin.concat(getBasin(data, col - 1, row, newBasin));
newBasin = newBasin.concat(getBasin(data, col + 1, row, newBasin));
newBasin = newBasin.concat(getBasin(data, col, row - 1, newBasin));
newBasin = newBasin.concat(getBasin(data, col, row + 1, newBasin));
return [...new Set(newBasin)];
}
export const part2 = (data: Array<DataType>): number => {
const lowPoints: Array<[number, number]> = [];
for (let row = 0; row < data.length; row += 1) {
for (let col = 0; col < data[row].length; col += 1) {
const neighbors = getNeighbors(data, row, col);
if (neighbors.every(n => n > data[row][col])) {
lowPoints.push([row, col]);
}
}
}
const basins: Array<number> = lowPoints.map(([row, col]) => {
return getBasin(data, col, row, []).length;
});
return product(basins.sort((a, b) => b - a).slice(0, 3));
};