72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
const fs = require('fs');
|
|
|
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
|
|
|
interface Coord { x: number, y: number };
|
|
|
|
const nodes: { [key: string]: Array<Coord> } = {};
|
|
const antiNodes: Array<Coord> = [];
|
|
|
|
let x = 0;
|
|
let y = 0;
|
|
for (let i = 0; i < input.length; i++) {
|
|
const char = input[i];
|
|
if (char === "\n") {
|
|
y++;
|
|
x = 0;
|
|
continue;
|
|
}
|
|
if (char !== '.') {
|
|
if (!nodes[char]) {
|
|
nodes[char] = [];
|
|
}
|
|
nodes[char].push({ x, y });
|
|
}
|
|
x++;
|
|
}
|
|
const width = x;
|
|
const height = y + 1;
|
|
|
|
function coordsMatch(a: Coord, b: Coord) {
|
|
return a.x === b.x && a.y === b.y;
|
|
}
|
|
|
|
function offMap(coord: Coord) {
|
|
return coord.x >= width
|
|
|| coord.x < 0
|
|
|| coord.y >= height
|
|
|| coord.y < 0;
|
|
}
|
|
|
|
function antiNodeExists(coord: Coord) {
|
|
return antiNodes.some((pos) => coordsMatch(pos, coord));
|
|
}
|
|
|
|
const total = Object.keys(nodes).length;
|
|
let current = 0;
|
|
|
|
for (let key of Object.keys(nodes)) {
|
|
current++;
|
|
console.log(Math.floor(current / total * 100));
|
|
const n = nodes[key];
|
|
for (let i = 0; i < n.length; i++) {
|
|
const nodeA = n[i];
|
|
const otherNodes = [...n.slice(0, i), ...n.slice(i+1)];
|
|
for (let j = 0; j < otherNodes.length; j++) {
|
|
const nodeB = otherNodes[j];
|
|
const xDiff = nodeB.x - nodeA.x;
|
|
const yDiff = nodeB.y - nodeA.y;
|
|
const antiXDiff = -1 * xDiff;
|
|
const antiYDiff = -1 * yDiff;
|
|
let antiCoord = { x: nodeB.x + antiXDiff, y: nodeB.y + antiYDiff };
|
|
while (!offMap(antiCoord)) {
|
|
if (!antiNodeExists(antiCoord)) {
|
|
antiNodes.push(antiCoord);
|
|
}
|
|
antiCoord = { x: antiCoord.x + antiXDiff, y: antiCoord.y + antiYDiff };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(antiNodes.length); |