Skip to content

Commit

Permalink
2024 day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardassassin committed Dec 8, 2024
1 parent 7a60de8 commit e161306
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
10 changes: 10 additions & 0 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,15 @@
"solveFile": "./year-2024/day-07/solve.js",
"inputFile": "./year-2024/day-07/input.txt",
"outputFile": "./year-2024/day-07/output.json"
},
{
"id": "2024-08",
"year": 2024,
"day": 8,
"yearFolder": "./year-2024",
"dayFolder": "./year-2024/day-08",
"solveFile": "./year-2024/day-08/solve.js",
"inputFile": "./year-2024/day-08/input.txt",
"outputFile": "./year-2024/day-08/output.json"
}
]
50 changes: 50 additions & 0 deletions year-2024/day-08/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.....8............1r.....a....................O...
.a..............4..q.........................0...9
....a.........8...................................
.................D.....................V0.........
.....d............................................
.r..........q....................................O
..................q...........................9...
..............D..............X..................V.
........D................X.................0......
.........8............X...........................
....................J....................9..0.....
..a..B............r..W........J...............R..Q
......WD...q.....1..........Q..............R..V...
.1W...................u...........................
..............................u.............R.....
....B..............d..c..................R........
.............J..............X............V........
......1...........................3...............
......B...........d...................3...........
............8..J.......u.....3....................
...........4.............6........................
.....4v.............d.......................O.....
..........................v.2.....................
.............................................s....
..................4...M..W..................s.....
......................m...........................
...........M......................................
..b..................c............................
....................Co..........KQ.......O.s......
.................C............................s...
.......x............c............................3
........o......A....U.....Q.........5.............
...............U..................j...5...........
.....K.......U................j..........2........
.......A.v.....w.....................c...5........
..K....................................j..........
...............K.yk....B.............2............
......C....b..............x...........Y...........
.....mA..C......U.................................
........M.....A.....................2..6...5......
.............................7.......Y............
.m.M......w..v....................................
............m...........x.....Y...................
....................k....w........................
......b.....w..S....7.............................
..............S..............x...........Y........
....................S...6.........................
.y...............S..........7.6.................9.
o..........k...............b......................
yo...........k....................................
4 changes: 4 additions & 0 deletions year-2024/day-08/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"partOne": 265,
"partTwo": 962
}
72 changes: 72 additions & 0 deletions year-2024/day-08/solve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* --- Day 8: Bridge Repair ---
*
* https://adventofcode.com/2024/day/8
*
* @param {string} input Text Input
* @returns Problem Solution
*/
export default function solve(input) {
const arr = input.split("\n").map((x) => x.split(""));

const points = new Map();
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[0].length; j++) {
const coord = [i, j];
const val = arr[i][j];
if (val === ".") continue;
if (!points.has(val)) points.set(val, []);
points.get(val).push(coord);
}
}
const locations = new Set();

const getDistance = (coord1, coord2) => [
coord2[0] - coord1[0],
coord2[1] - coord1[1],
];

const antidoteInfo = [];
for (const [key, value] of points.entries()) {
for (let i = 0; i < value.length; i++) {
for (let j = i + 1; j < value.length; j++) {
const val1 = value[i];
const val2 = value[j];
const dist = getDistance(val1, val2);
antidoteInfo.push([val1, dist]);
locations.add(`${val1[0] - dist[0]},${val1[1] - dist[1]}`);
locations.add(`${val2[0] + dist[0]},${val2[1] + dist[1]}`);
}
}
}
const inBounds = (i, j) =>
i >= 0 && i < arr.length && j >= 0 && j < arr[0].length;

const locations2 = [...locations]
.map((x) => x.split(","))
.filter((x) => inBounds(x[0], x[1]));

const partOne = locations2.length;
const addPoint = (i, j) => locations.add(`${i},${j}`);

for (const [start, diff] of antidoteInfo) {
let start2 = [...start];
while (inBounds(...start2)) {
addPoint(...start2);
start2[0] -= diff[0];
start2[1] -= diff[1];
}
start2 = [...start];
while (inBounds(...start2)) {
addPoint(...start2);
start2[0] += diff[0];
start2[1] += diff[1];
}
}

const partTwo = [...locations]
.map((x) => x.split(","))
.filter((x) => inBounds(x[0], x[1])).length;

return { partOne, partTwo };
}

0 comments on commit e161306

Please sign in to comment.