Skip to content

Commit 424a6d5

Browse files
committed
refactor: migrate 08/2024
1 parent 276eda8 commit 424a6d5

File tree

6 files changed

+77
-23
lines changed

6 files changed

+77
-23
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules
22
.DS_Store
3-
*.txt
3+
puzzle.txt

2024/07/example.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
190: 10 19
2+
3267: 81 40 27
3+
83: 17 5
4+
156: 15 6
5+
7290: 6 8 6 15
6+
161011: 16 10 13
7+
192: 17 8 14
8+
21037: 9 7 18 13
9+
292: 11 6 16 20

2024/08/example.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............

2024/08/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expect, describe, test } from "bun:test";
2+
import { part1, part2 } from ".";
3+
import { getInputs } from "../../utils/get-inputs";
4+
5+
const { exampleInput, puzzleInput } = await getInputs("2024/08");
6+
7+
describe("part 1", () => {
8+
test("example", () => {
9+
expect(part1(exampleInput)).toBe(14);
10+
});
11+
12+
test("puzzle", () => {
13+
expect(part1(puzzleInput)).toBe(269);
14+
});
15+
});
16+
17+
describe("part 2", () => {
18+
test("example", () => {
19+
expect(part2(exampleInput)).toBe(34);
20+
});
21+
22+
test("puzzle", () => {
23+
expect(part2(puzzleInput)).toBe(949);
24+
});
25+
});

2024/08/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import { getPuzzle } from "../../utils";
1+
import { timePart1, timePart2 } from "../../utils/time-part";
22

3-
const puzzleInput = getPuzzle(__dirname).trim();
3+
const parseInput = (input: string) =>
4+
input.split("\n").map((row) => row.split(""));
45

5-
const map = puzzleInput.split("\n").map((row) => row.split(""));
6+
const measureMap = (map: ReturnType<typeof parseInput>) => {
7+
const WIDTH = map[0].length;
8+
const HEIGHT = map.length;
69

7-
const WIDTH = map[0].length;
8-
const HEIGHT = map.length;
9-
10-
// Part 1
11-
(() => {
12-
console.time("part 1");
10+
return { WIDTH, HEIGHT };
11+
};
1312

13+
export const part1 = timePart1((input: string) => {
14+
const map = parseInput(input);
15+
const { HEIGHT, WIDTH } = measureMap(map);
1416
const antennas = new Map<string, Set<string>>();
1517

1618
for (let y = 0; y < HEIGHT; y++) {
@@ -22,7 +24,7 @@ const HEIGHT = map.length;
2224
}
2325

2426
if (antennas.has(cell)) {
25-
antennas.get(cell).add(`${x},${y}`);
27+
antennas.get(cell)!.add(`${x},${y}`);
2628
} else {
2729
antennas.set(cell, new Set<string>([`${x},${y}`]));
2830
}
@@ -83,13 +85,12 @@ const HEIGHT = map.length;
8385
}
8486
}
8587

86-
console.log("part 1 antinode count ::", antinodes.size);
87-
console.timeEnd("part 1");
88-
})();
88+
return antinodes.size;
89+
});
8990

90-
// Part 2
91-
(() => {
92-
console.time("part 2");
91+
export const part2 = timePart2((input: string) => {
92+
const map = parseInput(input);
93+
const { HEIGHT, WIDTH } = measureMap(map);
9394
const antennas = new Map<string, Set<string>>();
9495

9596
for (let y = 0; y < HEIGHT; y++) {
@@ -101,7 +102,7 @@ const HEIGHT = map.length;
101102
}
102103

103104
if (antennas.has(cell)) {
104-
antennas.get(cell).add(`${x},${y}`);
105+
antennas.get(cell)!.add(`${x},${y}`);
105106
} else {
106107
antennas.set(cell, new Set<string>([`${x},${y}`]));
107108
}
@@ -198,6 +199,5 @@ const HEIGHT = map.length;
198199
}
199200
}
200201

201-
console.log("part 2 antinode count ::", antinodes.size);
202-
console.timeEnd("part 2");
203-
})();
202+
return antinodes.size;
203+
});

utils/get-inputs.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
export const getInputs = async (basePath: string) => {
2-
const exampleInput = await Bun.file(`${basePath}/example.txt`).text();
3-
const puzzleInput = await Bun.file(`${basePath}/puzzle.txt`).text();
2+
let exampleInput: string = "";
3+
let puzzleInput: string = "";
4+
5+
try {
6+
exampleInput = await Bun.file(`${basePath}/example.txt`).text();
7+
} catch {}
8+
9+
try {
10+
puzzleInput = await Bun.file(`${basePath}/puzzle.txt`).text();
11+
} catch {}
412

513
return { exampleInput, puzzleInput };
614
};

0 commit comments

Comments
 (0)