-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.ts
33 lines (27 loc) · 923 Bytes
/
part2.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
import * as fs from 'fs';
const input = fs
.readFileSync('input', 'utf8')
.split('\n')
.map((x) => x.split(''));
let result = 0;
const rows = input.map((x) => x.join(''));
const columns = input[0].map((_, i) => input.map((x) => x[i]).join(''));
const galaxies = [];
const emptyRows = rows.map((_, i) => i).filter((x) => !rows[x].includes('#'));
const emptyColumns = columns.map((_, i) => i).filter((x) => !columns[x].includes('#'));
for (let i = 0; i < rows.length; i++) {
for (let j = 0; j < columns.length; j++) {
if (input[i][j] === '#') {
galaxies.push([
i + emptyRows.filter((x) => x < i).length * 999999,
j + emptyColumns.filter((x) => x < j).length * 999999,
]);
}
}
}
for (let i = 0; i < galaxies.length; i++) {
for (let j = i + 1; j < galaxies.length; j++) {
result += Math.abs(galaxies[i][0] - galaxies[j][0]) + Math.abs(galaxies[i][1] - galaxies[j][1]);
}
}
console.log(result);