Skip to content

Commit d8836c5

Browse files
committedDec 9, 2024·
d8 p1
1 parent c148e3d commit d8836c5

File tree

4 files changed

+153
-2
lines changed

4 files changed

+153
-2
lines changed
 

‎inputs/8.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
..................................................
2+
.r................................................
3+
..........................I.......................
4+
........................I.........................
5+
................................................M.
6+
............h......................A..............
7+
..7....................I.........h................
8+
......7..................................M....9...
9+
.o.....U..........................................
10+
......................................O...........
11+
....c.................J................O...M...A..
12+
..................................................
13+
...R...7..........................................
14+
..............r...................................
15+
...................J..................9...........
16+
...7..K......UJ...................................
17+
......0...U.........................x.............
18+
.......R.......0..B......................x........
19+
.......................k.....Z.......9............
20+
.......L.........I.....J............m.............
21+
.....K.BR........r.0.C............................
22+
.......K.BR......c................................
23+
..................h....m....Al...........H........
24+
..............L..k.......h...m..........x..9......
25+
........................Z.....m........xO.........
26+
..........0................l......................
27+
.6..................b.............................
28+
............k...o..............Z..................
29+
........4.....o...........L.......................
30+
....................Xo............................
31+
...........8..B..L.........i......................
32+
..z...............bX..........A...................
33+
j........z...X......C.......i........5............
34+
.b...H6.......................U.......l...........
35+
..................X...............................
36+
...6......................Z..........a............
37+
....6........c............5.........1.............
38+
.4.......................5........................
39+
..........k.......H1l.............................
40+
2.................C.......i...................u...
41+
.............a....2...............................
42+
.....z....H.......1..8.....................u......
43+
...........j...b..................................
44+
3.........j.........................a.............
45+
...4................a.............................
46+
..M................j.....1..........5.............
47+
............................................u.....
48+
..4..3...........i................................
49+
z3.................2..............................
50+
..........8..................2.C..................

‎src/7.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function solution(input: string): { part1: string; part2: string } {
5959
if (index === nums.length) {
6060
return;
6161
}
62-
console.log(a, b, index);
62+
//console.log(a, b, index);
6363

6464
const alpha = calculate(a, b, "+");
6565
const beta = calculate(a, b, "*");
@@ -81,7 +81,7 @@ function solution(input: string): { part1: string; part2: string } {
8181
}
8282
};
8383

84-
bar(nums[0], nums[1], 1);
84+
//bar(nums[0], nums[1], 1);
8585
});
8686

8787
const sum = (acc: number, curr: number) => acc + curr;

‎src/8.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function solution(input: string): { part1: string; part2: string } {
2+
let result1 = 0;
3+
let result2 = 0;
4+
5+
const lines = input.split("\n").map((line) => line.split(""));
6+
7+
const antennaCoordinates = new Map<string, Set<string>>();
8+
9+
lines.forEach((line, y) => {
10+
line.forEach((cell, x) => {
11+
if (cell !== ".") {
12+
if (!antennaCoordinates.has(cell)) {
13+
antennaCoordinates.set(cell, new Set<string>());
14+
}
15+
const antennas = antennaCoordinates.get(cell)!;
16+
antennaCoordinates.set(cell, antennas.add(`${x},${y}`));
17+
}
18+
});
19+
});
20+
21+
const antennaTypes = antennaCoordinates.keys();
22+
const antinodeCoordinates = new Set<string>();
23+
24+
for (const type of antennaTypes) {
25+
const coordinates = Array.from(antennaCoordinates.get(type)!).map((coord) =>
26+
coord.split(",").map((c) => parseInt(c))
27+
);
28+
29+
let pointer = 0;
30+
31+
while (pointer < coordinates.length) {
32+
const [x, y] = coordinates[pointer];
33+
34+
coordinates.forEach(([x2, y2]) => {
35+
if (x === x2 && y === y2) {
36+
return;
37+
}
38+
39+
const dx = x2 - x;
40+
const dy = y2 - y;
41+
42+
const nodeX = x - dx;
43+
const nodeY = y - dy;
44+
45+
if (
46+
nodeX < 0 ||
47+
nodeY < 0 ||
48+
nodeX >= lines[0].length ||
49+
nodeY >= lines.length
50+
) {
51+
return;
52+
}
53+
54+
const antinode = `${nodeX},${nodeY}`;
55+
antinodeCoordinates.add(antinode);
56+
});
57+
58+
pointer++;
59+
}
60+
}
61+
62+
result1 = antinodeCoordinates.size;
63+
64+
return {
65+
part1: result1.toString(),
66+
part2: result2.toString(),
67+
};
68+
}
69+
70+
export default solution;

‎test/8.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect, test, describe } from "bun:test";
2+
import solution from "../src/8";
3+
4+
const EXAMPLE_1 = `............
5+
........0...
6+
.....0......
7+
.......0....
8+
....0.......
9+
......A.....
10+
............
11+
............
12+
........A...
13+
.........A..
14+
............
15+
............`;
16+
const RESULT_1 = "14";
17+
18+
const EXAMPLE_2 = EXAMPLE_1;
19+
const RESULT_2 = "31";
20+
21+
describe("Day 8", () => {
22+
test("Part 1", () => {
23+
const result = solution(EXAMPLE_1);
24+
expect(result.part1).toBe(RESULT_1);
25+
});
26+
27+
test("Part 2", () => {
28+
const result = solution(EXAMPLE_2);
29+
expect(result.part2).toBe(RESULT_2);
30+
});
31+
});

0 commit comments

Comments
 (0)
Please sign in to comment.