Skip to content

Commit 4774d0e

Browse files
committed
refactor: migrate 09/2024
1 parent 424a6d5 commit 4774d0e

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

2024/09/example.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2333133121414131402

2024/09/index.test.ts

+25
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/09");
6+
7+
describe("part 1", () => {
8+
test("example", () => {
9+
expect(part1(exampleInput)).toBe(1928);
10+
});
11+
12+
test("puzzle", () => {
13+
expect(part1(puzzleInput)).toBe(6258319840548);
14+
});
15+
});
16+
17+
describe("part 2", () => {
18+
test("example", () => {
19+
expect(part2(exampleInput)).toBe(2858);
20+
});
21+
22+
test("puzzle", () => {
23+
expect(part2(puzzleInput)).toBe(6286182965311);
24+
});
25+
});

2024/09/index.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { getPuzzle } from "../../utils";
1+
import { timePart1, timePart2 } from "../../utils/time-part";
22

3-
const puzzleInput = getPuzzle(__dirname).trim();
4-
const diskMap = puzzleInput.split("");
3+
const parseInput = (input: string) => input.split("");
54

65
// Part 1
7-
(() => {
8-
console.time("part 1");
6+
export const part1 = timePart1((input: string) => {
7+
const diskMap = parseInput(input);
98
let representation = "";
109

1110
for (let i = 0; i < diskMap.length; i++) {
@@ -26,7 +25,7 @@ const diskMap = puzzleInput.split("");
2625
let lastFileBlockIndex = representationArr.findLastIndex((e) => e !== ".");
2726

2827
while (lastFileBlockIndex > firstFreeSpaceBlockIndex) {
29-
const lastEntry = representationArr.pop();
28+
const lastEntry = representationArr.pop()!;
3029

3130
if (lastEntry !== ".") {
3231
representationArr[firstFreeSpaceBlockIndex] = lastEntry;
@@ -50,13 +49,11 @@ const diskMap = puzzleInput.split("");
5049
}
5150
}
5251

53-
console.log("part 1 checksum ::", checksum);
54-
console.timeEnd("part 1");
55-
})();
52+
return checksum;
53+
});
5654

57-
// Part 2
58-
(() => {
59-
console.time("part 2");
55+
export const part2 = timePart2((input: string) => {
56+
const diskMap = parseInput(input);
6057
let representation = "";
6158

6259
for (let i = 0; i < diskMap.length; i++) {
@@ -145,6 +142,5 @@ const diskMap = puzzleInput.split("");
145142
}
146143
}
147144

148-
console.log("part 2 checksum ::", checksum);
149-
console.timeEnd("part 2");
150-
})();
145+
return checksum;
146+
});

0 commit comments

Comments
 (0)