-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.js
67 lines (59 loc) · 1.52 KB
/
part2.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
const { readFileSync } = require("fs");
// Parse Input
const inputFile = "input.txt";
const input = readFileSync(inputFile).toString();
const grid = [];
input
.trim()
.split("\n")
.forEach((line) => grid.push(line.split("")));
let antenaPositions = [];
for (let y = 0; y < grid.length; y++) {
const line = grid[y];
for (let x = 0; x < line.length; x++) {
const element = line[x];
if (element != ".") {
antenaPositions.push({
id: antenaPositions.length,
type: element,
x,
y,
});
}
grid[y][x] = element;
}
}
function isInGrid(x, y) {
return x >= 0 && y >= 0 && x < grid[0].length && y < grid.length;
}
const antinodes = [...antenaPositions];
antenaPositions.forEach((a) => {
antenaPositions
.filter((ca) => a.type == ca.type && ca.id != a.id)
.forEach((o) => {
let xDiff = o.x - a.x;
let yDiff = o.y - a.y;
let antinode = { type: a.type, x: a.x - xDiff, y: a.y - yDiff };
if (xDiff == 0 && yDiff == 0) {
antinodes.push(antinode);
} else {
while (true) {
if (isInGrid(antinode.x, antinode.y)) {
if (
!antinodes.find((n) => n.x == antinode.x && n.y == antinode.y)
) {
antinodes.push(antinode);
}
antinode = {
type: a.type,
x: antinode.x - xDiff,
y: antinode.y - yDiff,
};
} else {
break;
}
}
}
});
});
console.log(antinodes.length);