-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8b.js
67 lines (52 loc) · 1.59 KB
/
8b.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
import {iterGrid, readInput} from "./lib.js";
const grid = readInput(8).map(line => line.split(''));
const antennas = {};
const antiAntennas = new Set();
iterGrid(grid, (cell, x, y) => {
if (cell === '.') {
return;
}
antennas[cell] = antennas[cell] || [];
antennas[cell].push({x, y});
});
for (const [antenna, positions] of Object.entries(antennas)) {
if (positions.length < 2) {
console.log(antenna);
continue;
}
for (const combination of combinations(positions)) {
const [a, b] = combination;
const dx = b.x - a.x;
const dy = b.y - a.y;
const antiA = {
x: a.x - dx,
y: a.y - dy
};
const antiB = {
x: b.x + dx,
y: b.y + dy
};
antiAntennas.add(`${a.x},${a.y}`);
antiAntennas.add(`${b.x},${b.y}`);
while (antiA.x >= 0 && antiA.y >= 0 && antiA.x < grid[0].length && antiA.y < grid.length) {
antiAntennas.add(`${antiA.x},${antiA.y}`);
antiA.x -= dx;
antiA.y -= dy;
}
while (antiB.x >= 0 && antiB.y >= 0 && antiB.x < grid[0].length && antiB.y < grid.length) {
antiAntennas.add(`${antiB.x},${antiB.y}`);
antiB.x += dx;
antiB.y += dy;
}
}
}
function combinations (positions) {
const result = [];
for (let i = 0; i < positions.length; i++) {
for (let j = i + 1; j < positions.length; j++) {
result.push([positions[i], positions[j]]);
}
}
return result;
}
console.log([...antiAntennas].sort());