forked from tpatel/advent-of-code-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.js
81 lines (69 loc) · 1.9 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
const fs = require("fs");
const lines = fs
.readFileSync("day09.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.split("\n") // Split on newline
.filter(Boolean); // Remove empty lines
function part1() {
let risk = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (let j = 0; j < line.length; j++) {
const current = line[j];
if (
(!(i - 1 >= 0) || current < lines[i - 1][j]) &&
(!(i + 1 < lines.length) || current < lines[i + 1][j]) &&
(!(j - 1 >= 0) || current < lines[i][j - 1]) &&
(!(j + 1 < line.length) || current < lines[i][j + 1])
) {
risk += Number(current) + 1;
}
}
}
console.log(risk);
}
part1();
function floodfill(i, j, map) {
if (map[i][j] === 1) return 0; // check node hasn't been visited
map[i][j] = 1; // mark node as visited
// count neighbors
let size = 1;
if (i - 1 >= 0) {
size += floodfill(i - 1, j, map);
}
if (i + 1 < map.length) {
size += floodfill(i + 1, j, map);
}
if (j - 1 >= 0) {
size += floodfill(i, j - 1, map);
}
if (j + 1 < map[i].length) {
size += floodfill(i, j + 1, map);
}
return size;
}
function part2() {
const map = Array(lines.length)
.fill(0)
.map((x, i) =>
Array(lines[0].length)
.fill(0)
.map((x, j) => (lines[i][j] === "9" ? 1 : 0))
);
let bassins = [];
// console.log(map.map((x) => x.join``).join`\n`);
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (let j = 0; j < line.length; j++) {
const size = floodfill(i, j, map);
if (size > 0) {
// console.log("");
// console.log(map.map((x) => x.join``).join`\n`);
bassins.push(size);
}
}
}
bassins.sort((a, b) => b - a);
console.log(bassins[0] * bassins[1] * bassins[2]);
}
part2();