-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a60de8
commit e161306
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.................................... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"partOne": 265, | ||
"partTwo": 962 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |