-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.js
58 lines (48 loc) · 1.58 KB
/
day11.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
const readFrom = require("./read-input");
const args = process.argv;
const input = readFrom(args);
if (!input) return;
const map = input.split("\n").map((line) => line.split(""));
const expandableRow = Array(map.length).fill(true);
const expandableCol = Array(map[0].length).fill(true);
const galaxyCords = [];
map.forEach((line, i) =>
line.forEach((s, j) => {
if (s === "#") {
galaxyCords.push([i, j]);
expandableRow[i] = false;
expandableCol[j] = false;
}
})
);
const getResultTest = (expandSize) => {
let result = 0;
for (let i = 0; i < galaxyCords.length; i++) {
for (let j = i + 1; j < galaxyCords.length; j++) {
const [startRow, startCol] = galaxyCords[i];
const [destRow, destCol] = galaxyCords[j];
let expandedRows = 0;
for (let k = startRow; k < destRow; k++) {
expandedRows += expandableRow[k] ? expandSize - 1 : 0;
}
let expandedCols = 0;
for (
let k = Math.min(startCol, destCol);
k < Math.max(startCol, destCol);
k++
) {
expandedCols += expandableCol[k] ? expandSize - 1 : 0;
}
result +=
Math.abs(startRow - destRow) +
Math.abs(startCol - destCol) +
expandedCols +
expandedRows;
}
}
return result;
};
const partOneResult = getResultTest(2);
console.log(partOneResult);
const partTwoResult = getResultTest(1000000);
console.log(partTwoResult);