Day 8
This commit is contained in:
@@ -35,14 +35,14 @@ for (let i = 0; i <= input.length; i++) {
|
|||||||
}
|
}
|
||||||
x++;
|
x++;
|
||||||
}
|
}
|
||||||
const width = x + 1;
|
const width = x;
|
||||||
const height = y + 1;
|
const height = y + 1;
|
||||||
const guardStart = guard.pos;
|
const guardStart = guard.pos;
|
||||||
|
|
||||||
|
|
||||||
const positions: Array<Coord> = [guard.pos];
|
const positions: Array<Coord> = [guard.pos];
|
||||||
|
|
||||||
function coodsMatch(a: Coord, b: Coord) {
|
function coordsMatch(a: Coord, b: Coord) {
|
||||||
return a.x === b.x && a.y === b.y;
|
return a.x === b.x && a.y === b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ function rotateGuard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isObs(coord: Coord, otherObs: Array<Coord>|null = null) {
|
function isObs(coord: Coord, otherObs: Array<Coord>|null = null) {
|
||||||
return (otherObs || obs).some((obsCoord) => coodsMatch(obsCoord, coord));
|
return (otherObs || obs).some((obsCoord) => coordsMatch(obsCoord, coord));
|
||||||
}
|
}
|
||||||
|
|
||||||
function offMap(coord: Coord) {
|
function offMap(coord: Coord) {
|
||||||
@@ -83,7 +83,7 @@ function offMap(coord: Coord) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function alreadyVisited(coord: Coord) {
|
function alreadyVisited(coord: Coord) {
|
||||||
return positions.some((pos) => coodsMatch(pos, coord));
|
return positions.some((pos) => coordsMatch(pos, coord));
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
72
2024/08/index.ts
Normal file
72
2024/08/index.ts
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
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);
|
||||||
Reference in New Issue
Block a user