-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.js
145 lines (120 loc) · 3.71 KB
/
day09.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { data, test } = require('./data/day09');
const parseData = (input) => (
input.map((line) => line.split('').map(Number))
);
const getAdjacents = (x, y, grid, horizontal) => (
horizontal
? [x - 1, x, x + 1].filter(el => el >= 0 && el < grid[0].length)
: [y - 1, y, y + 1].filter(el => el >= 0 && el < grid.length)
);
const determineIfLowPoint = (x, y, grid) => {
const horizontalAdjacent = getAdjacents(x, y, grid, true);
const verticalAdjacent = getAdjacents(x, y, grid);
const notLessThanAdjacents = [];
const point = grid[y][x];
for (let i = 0; i < verticalAdjacent.length; i++) {
for (let j = 0; j < horizontalAdjacent.length; j++) {
const candidateY = verticalAdjacent[i];
const candidateX = horizontalAdjacent[j];
const candidatePoint = grid[candidateY][candidateX];
// No diagonals
if (candidateY !== y && candidateX !== x) {
continue;
}
if (candidatePoint < point) {
return false;
}
notLessThanAdjacents.push(candidatePoint);
}
}
// Check for plateau
if (notLessThanAdjacents.every(t => t === point)) {
return false;
}
return true;
}
const findLowPoints = (input) => {
let lowPoints = [];
input.forEach((row, i) => {
row.forEach((col, j) => {
if (determineIfLowPoint(j, i, input)) {
lowPoints.push({ point: col, x: j, y: i , toString: () => `${j},${i}` });
}
});
});
return lowPoints;
};
const getRiskLevel = (input) => (
findLowPoints(input).reduce((sum, { point: t }) => sum + t + 1, 0)
);
const arrayUnique = (array) => {
const uniqueStrs = Object.values(array).map(el => el.toString()).filter((value, idx, arr) => arr.indexOf(value) === idx);
return uniqueStrs.map(str => array.find(el => el.toString() === str));
};
const getBasinFromLowPoint = (lowPoint, grid, visited = []) => {
const { x, y, point } = lowPoint;
const basinCandidates = [];
if (visited.some(el => el.x === x && el.y === y)) {
return basinCandidates;
}
// up
if (y !== 0) {
if (grid[y - 1][x] !== 9 && grid[y - 1][x] >= point) {
basinCandidates.push({ point: grid[y - 1][x], x, y: y - 1, toString: () => `${x},${y - 1}` });
}
}
// right
if (x < grid[0].length - 1) {
if (grid[y][x + 1] !== 9 && grid[y][x + 1] >= point) {
basinCandidates.push({ point: grid[y][x + 1], x: x + 1, y, toString: () => `${x + 1},${y}` });
}
}
// down
if (y < grid.length - 1) {
if (grid[y + 1][x] !== 9 && grid[y + 1][x] >= point) {
basinCandidates.push({ point: grid[y + 1][x], x, y: y + 1, toString: () => `${x},${y + 1}` });
}
}
// left
if (x !== 0) {
if (grid[y][x - 1] !== 9 && grid[y][x - 1] >= point) {
basinCandidates.push({ point: grid[y][x - 1], x: x - 1, y, toString: () => `${x - 1},${y}` });
}
}
return arrayUnique([
lowPoint,
...(
[]
.concat(
...basinCandidates.map(candidate => getBasinFromLowPoint(candidate, grid, [...visited, lowPoint]))
)
),
]);
};
const getThreeLargestBasins = (grid) => {
const lowPoints = findLowPoints(grid);
return lowPoints
.map((low, idx, arr) => {
const basin = getBasinFromLowPoint(low, grid)
console.log(`${idx + 1}/${arr.length}: ${basin.length}`);
return basin;
})
.map(arr => arr.length)
.sort((a, b) => b - a)
.slice(0, 3);
};
const getThreeLargestBasinsProduct = (grid) => (
getThreeLargestBasins(grid).reduce((prod, x) => prod * x, 1)
);
const parsedTest = parseData(test);
const parsedData = parseData(data);
console.log({
test: {
a: getRiskLevel(parsedTest),
b: getThreeLargestBasinsProduct(parsedTest),
},
data: {
a: getRiskLevel(parsedData),
b: getThreeLargestBasinsProduct(parsedData),
},
});