diff --git a/2024/06/index.ts b/2024/06/index.ts index d66a224..4a70cbb 100644 --- a/2024/06/index.ts +++ b/2024/06/index.ts @@ -35,14 +35,14 @@ for (let i = 0; i <= input.length; i++) { } x++; } -const width = x + 1; +const width = x; const height = y + 1; const guardStart = guard.pos; const positions: Array = [guard.pos]; -function coodsMatch(a: Coord, b: Coord) { +function coordsMatch(a: Coord, b: Coord) { return a.x === b.x && a.y === b.y; } @@ -72,7 +72,7 @@ function rotateGuard() { } function isObs(coord: Coord, otherObs: Array|null = null) { - return (otherObs || obs).some((obsCoord) => coodsMatch(obsCoord, coord)); + return (otherObs || obs).some((obsCoord) => coordsMatch(obsCoord, coord)); } function offMap(coord: Coord) { @@ -83,7 +83,7 @@ function offMap(coord: Coord) { } function alreadyVisited(coord: Coord) { - return positions.some((pos) => coodsMatch(pos, coord)); + return positions.some((pos) => coordsMatch(pos, coord)); } while (true) { diff --git a/2024/08/index.ts b/2024/08/index.ts new file mode 100644 index 0000000..01646f3 --- /dev/null +++ b/2024/08/index.ts @@ -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 } = {}; +const antiNodes: Array = []; + +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); \ No newline at end of file