-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.js
52 lines (45 loc) · 1.16 KB
/
part1.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
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.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 (
isInGrid(antinode.x, antinode.y) &&
!antinodes.find((n) => n.x == antinode.x && n.y == antinode.y)
) {
antinodes.push(antinode);
}
});
});
console.log(antinodes.length);