Skip to content

Commit

Permalink
2024 day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
pvainio committed Dec 8, 2024
1 parent ffc7f23 commit 316b656
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 2024/input/8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.....y..........................p................r
........I.........................................
......................4.s.........................
..........4.......................................
....y.............................................
......................................p.........r.
..........0..s......N..................1.....p....
..y........4.......................p..............
...............0..................................
..............0....t....N....h....................
.............N....................................
......j...................s............H...l..O...
..........q.................H................O....
..f...e.qj.....y...0..............................
...........t..........................k..Q..r.....
.........6................Q..s...x......W.........
....2..b...e....t..4.........c.....xW.j...........
...e....................w................1.....O..
..e..j..5...........................c.............
.........B..2...............MK................H...
...2......b...g..X...q..........h...............O.
...q...2..........m....k...i...............QV.x...
...................i.........W.k.............HQ...
........b...X...............D..........c...N......
................................l..........h.....I
.m...........g......l.......c.............3......V
....X.......m........g...V.K...7......F.d.........
.........b.X...U..........................C.......
.....................l..............o.1....C......
............u.............K..............3...d....
......................i.T....f................V...
..............................1.k.................
.B.....E......9..m....K..5.M......................
...P...............M...95....o..i........I........
...............................S......3......wI...
.....EP...........9........5..T.R.................
.P..........v..9......f.............R.Co..w3......
..........h...SG..v.E...7..f.T....................
..........6..........L.................Y.......d..
..........B...............U........D..............
....B................U.....8..M....n...J..........
.........................L................Fw......
....L6E.P.................7.UG....J.....Y.D.......
........t........v...SJ........n..d...............
......................8v.....uG...................
..................L.....n.........................
...............T..............n......D............
..............o.........8................J.Y.R....
..................S...............u....F.......R..
........6..............u.....7.8..........Y..F....
42 changes: 42 additions & 0 deletions 2024/js/day8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { asLines } from './util.js';

const map = asLines('../input/8.txt').map(l => l.split(""));

const antennaByFreq = {};
map.forEach((r, y) => r.forEach((freq, x) => {
if (freq == '.') return;
if (!antennaByFreq[freq]) antennaByFreq[freq] = [];
antennaByFreq[freq].push([x, y]);
}));

const valid = ([x, y]) => x >= 0 && x < map[0].length && y >= 0 && y < map.length;

const anti = ([x, y], [dx, dy], distanceLimit) => {
const anti = new Set();
for (const an = [x, y]; valid(an); an[0] += dx, an[1] += dy) {
if (distanceLimit && an[0] - dx != x && an[1] - dy != y) continue;
anti.add(JSON.stringify(an));
}
return anti;
};

const antinodes = (distanceLimit) => {
const antinodes = new Set();
Object.entries(antennaByFreq).forEach(([_, coords]) => {
for (let i = 0; i < coords.length; i++) {
for (let j = 0; j < coords.length; j++) {
if (i == j) continue;
const [x1, y1] = coords[i];
const [x2, y2] = coords[j];
const dx = x2 - x1;
const dy = y2 - y1;
anti([x1, y1], [-dx, -dy], distanceLimit).forEach(a => antinodes.add(a));
anti([x2, y2], [dx, dy], distanceLimit).forEach(a => antinodes.add(a));
}
}
});
return antinodes;
}

console.log(`day8a ${antinodes(true).size}`);
console.log(`day8b ${antinodes(false).size}`);

0 comments on commit 316b656

Please sign in to comment.