-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.mjs
127 lines (113 loc) · 2.9 KB
/
tasks.mjs
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
// Floodfilling.
import input from './input.mjs';
// const input = [2199943210,3987894921,9856789892,8767896789,9899965678]
const map = input.map((x) => [...x.toString()]);
// Task 1 identifies lowest poiints, and adds their score together
function task1(map) {
const lowestPoints = map.flatMap((row, y) => {
return row.filter((cell, x) => {
const offsets = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
return offsets.every((offset) => {
const x2 = x + offset[0];
const y2 = y + offset[1];
if (x2 < 0 || y2 < 0 || x2 >= map[0].length || y2 >= map.length) {
return true;
}
return map[y2][x2] > cell;
});
});
});
return lowestPoints.reduce(
(acc, point) => acc + 1 + Number.parseInt(point),
0,
);
}
console.log(task1(map));
// task 2 finds the lowest points, and then backfills them up to points at height 9 and determines the size of the area
function task2(map) {
const mapObjects = objectifyMap(map);
const lowestPoints = findLowestPoints(mapObjects);
const basins = lowestPoints.map((point) => {
return flood(mapObjects, ...point);
});
const largestBasins = basins.sort((a, b) => b - a).slice(0, 3);
displayMap(mapObjects);
return largestBasins.reduce((acc, basin) => acc * basin);
}
function objectifyMap(map) {
return map.map((row) => {
return row.map((cell) => {
return {
height: cell,
filled: false,
};
});
});
}
function findLowestPoints(map) {
const lowestPoints = map.flatMap((row, y) => {
return row.reduce((acc, cell, x) => {
const offsets = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
if (
offsets.every((offset) => {
const x2 = x + offset[0];
const y2 = y + offset[1];
if (x2 < 0 || y2 < 0 || x2 >= map[0].length || y2 >= map.length) {
return true;
}
return map[y2][x2].height > cell.height;
})
)
acc.push([y, x]);
return acc;
}, []);
});
return lowestPoints;
}
function flood(map, y, x) {
if (map[y][x].filled) return 0;
if (map[y][x].height >= 9) return 0;
map[y][x].filled = true;
const offsets = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
return offsets.reduce((acc, offset) => {
const x2 = x + offset[0];
const y2 = y + offset[1];
if (x2 < 0 || y2 < 0 || x2 >= map[0].length || y2 >= map.length) {
return acc;
}
return acc + flood(map, y2, x2);
}, 1);
}
function displayMap(map) {
let string = '\n\n';
map.forEach((row) => {
row.forEach((cell) => {
if (cell.filled) {
string += '\x1b[34m#\x1b[89m';
} else {
string += `\x1b[97m${cell.height}\x1b[39m`;
}
});
string += '\n';
});
console.clear();
console.log(string);
}
console.time('task2');
console.log(task2(map));
console.timeEnd('task2');