-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonestar_day8.js
More file actions
72 lines (58 loc) · 1.58 KB
/
onestar_day8.js
File metadata and controls
72 lines (58 loc) · 1.58 KB
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
const fs = require("fs");
const matxir = fs.readFileSync("./day_08.in", "utf8").trim().split("\n");
const n = matxir?.length;
function limite(x, y) {
return x >= 0 && x < n && y >= 0 && y < n;
}
function* getAntinodos(a, b) {
const [ax, ay] = a;
const [bx, by] = b;
const cx = ax - (bx - ax);
const cy = ay - (by - ay);
const dx = bx + (bx - ax);
const dy = by + (by - ay);
if (limite(cx, cy)) {
yield [cx, cy];
}
if (limite(dx, dy)) {
yield [dx, dy];
}
}
const antinodos = new Set();
const locaciones = {};
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (matxir[i][j] !== ".") {
if (!locaciones[matxir[i][j]]) {
locaciones[matxir[i][j]] = [];
}
locaciones[matxir[i][j]].push([i, j]);
}
}
}
function getCombinaciones(array, r) {
const results = [];
function fnTemp(start, combo) {
if (combo?.length === r) {
results.push([...combo]);
return;
}
for (let i = start; i < array?.length; i++) {
combo.push(array[i]);
fnTemp(i + 1, combo);
combo.pop();
}
}
fnTemp(0, []);
return results;
}
for (const freq in locaciones) {
const locs = locaciones[freq];
const pairs = getCombinaciones(locs, 2);
for (const [a, b] of pairs) {
for (const antinode of getAntinodos(a, b)) {
antinodos?.add(JSON.stringify(antinode)); // JSON.stringify para poder almacenar coordenadas únicas
}
}
}
console.log(antinodos?.size);