Skip to content

Commit

Permalink
Day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Av32000 committed Dec 8, 2024
1 parent 5c3eea3 commit a9432b1
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
52 changes: 52 additions & 0 deletions 08/part1.js
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);
67 changes: 67 additions & 0 deletions 08/part2.js
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);

0 comments on commit a9432b1

Please sign in to comment.