-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (101 loc) · 2.88 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import run from "aocrunner";
import { transpose } from "../utils/array.js";
import { abs } from "mathjs";
const parseInput = (rawInput) => rawInput.split("\n").map((x) => x.split(""));
function calcDistances(input, emptyMultiplier) {
let galaxy = [];
let emptyYs = [];
let emptyXs = [];
let galaxyCount = 0;
input.forEach((line, i) => {
line.forEach((char, j) => {
if (char === "#") input[i][j] = galaxyCount++;
});
});
for (let y = 0; y < input.length; y++) {
const row = input[y];
galaxy.push(row);
if (
row.every((val) => {
return val === ".";
})
) {
emptyYs.push(y);
}
}
const galaxyTp = transpose(galaxy);
galaxy = [];
for (let x = 0; x < galaxyTp.length; x++) {
const col = galaxyTp[x];
galaxy.push(col);
if (col.every((val) => val === ".")) {
emptyXs.push(x);
}
}
galaxy = transpose(galaxy);
const coordinates = [];
for (let y = 0; y < galaxy.length; y++) {
for (let x = 0; x < galaxy[y].length; x++) {
if (/\d+/g.test(galaxy[y][x])) {
coordinates.push([x, y]);
}
}
}
let sum = 0;
for (let i = 0; i < coordinates.length; i++) {
const coords1 = coordinates[i];
for (let j = i + 1; j < coordinates.length; j++) {
const coords2 = coordinates[j];
const filteredEmptyX = emptyXs.filter(
(val) =>
(coordinates[i][0] < val && val < coordinates[j][0]) ||
(coordinates[i][0] > val && val > coordinates[j][0]),
);
const filteredEmptyY = emptyYs.filter(
(val) =>
(coordinates[i][1] < val && val < coordinates[j][1]) ||
(coordinates[i][1] > val && val > coordinates[j][1]),
);
const emptiesX =
filteredEmptyX.length * emptyMultiplier - filteredEmptyX.length;
const emptiesY =
filteredEmptyY.length * emptyMultiplier - filteredEmptyY.length;
const spaceX = emptiesX > 0 ? emptiesX : 0;
const spaceY = emptiesY > 0 ? emptiesY : 0;
const deltaX = abs(coords1[0] - coords2[0]) + spaceX;
const deltaY = abs(coords1[1] - coords2[1]) + spaceY;
sum += deltaX + deltaY;
}
}
return sum;
}
const part1 = (rawInput) => {
const input = parseInput(rawInput);
return calcDistances(input, 2);
};
const part2 = (rawInput) => {
const input = parseInput(rawInput);
return calcDistances(input, 1000000);
};
run({
part1: {
tests: [
{
input: `...#......\n.......#..\n#.........\n..........\n......#...\n.#........\n.........#\n..........\n.......#..\n#...#.....`,
expected: 374,
},
],
solution: part1,
},
part2: {
tests: [
// {
// input: `...#......\n.......#..\n#.........\n..........\n......#...\n.#........\n.........#\n..........\n.......#..\n#...#.....`,
// expected: 1030,
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});