-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11_1.js
104 lines (97 loc) · 2.3 KB
/
day11_1.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
const { parse } = require('path');
const path = require('path');
const fs = require('fs');
const lineReader = require('readline').createInterface({
input: fs.createReadStream('input.txt'),
});
const data = [];
let galaxies = [];
let row = 0;
let paths = [];
function getGalaxyAtPosition(row, col) {
let galaxyIndex = 0;
galaxies.forEach((galaxy, index) => {
if (galaxy[0] === row && galaxy[1] === col) {
galaxyIndex = index;
return;
}
});
return galaxyIndex;
}
function dfs(galaxy) {
const visited = Array.from({ length: data.length }, () =>
Array(data[0].length).fill(false)
);
const queue = [[galaxy[0], galaxy[1], 0]];
const pathLengths = Array(galaxies.length).fill(0);
while (queue.length !== 0) {
const point = queue.shift();
const row = point[0];
const col = point[1];
const length = point[2];
if (
row < 0 ||
row >= data.length ||
col < 0 ||
col >= data[0].length ||
visited[row][col]
) {
continue;
}
if (data[row][col] === '#') {
const index = getGalaxyAtPosition(row, col);
pathLengths[index] = length;
}
visited[row][col] = true;
queue.push([row + 1, col, length + 1]);
queue.push([row - 1, col, length + 1]);
queue.push([row, col + 1, length + 1]);
queue.push([row, col - 1, length + 1]);
}
paths.push(pathLengths);
}
lineReader.on('line', function (line) {
data.push(line);
const y = line.indexOf('#');
if (y === -1) {
data.push(line);
}
row++;
});
lineReader.on('close', function () {
let j = 0;
while (j < data[0].length) {
const column = [];
for (let i = 0; i < data.length; i++) {
column.push(data[i][j]);
}
if (column.includes('#')) {
j++;
continue;
}
for (let i = 0; i < data.length; i++) {
const newRow = [...data[i]];
newRow.splice(j, 0, '.');
data[i] = newRow;
}
j += 2;
}
galaxies = [];
for (let i = 0; i < data.length; i++) {
for (let j = 0; j < data[0].length; j++) {
if (data[i][j] === '#') {
galaxies.push([i, j]);
}
}
}
galaxies.forEach((galaxy) => {
dfs(galaxy);
});
let sum = 0;
for (let i = 0; i < paths.length; i++) {
for (let j = i + 1; j < paths[0].length; j++) {
sum += paths[i][j];
}
}
console.log(sum);
});