-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); |