Skip to content

Commit

Permalink
feat: update package scripts to create new days
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktoriavh committed Dec 5, 2024
1 parent 34c3059 commit d9b10de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"typescript": "^5.0.0"
},
"scripts": {
"start": "bun run ./src/index.ts",
"start": "bun run ./script/index.ts",
"create": "bun run ./script/newDay.ts",
"test": "bun test ./test/* --watch"
}
}
40 changes: 27 additions & 13 deletions src/index.ts β†’ script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Solution = {
console.log("Advent of Code 2024");

const inputPath = "./inputs/{{day}}.txt";
const solutionPath = "./{{day}}.ts";
const solutionPath = "../src/{{day}}.ts";

export async function solveDay(day: number): Promise<Solution> {
try {
Expand All @@ -29,22 +29,36 @@ export async function solveDay(day: number): Promise<Solution> {

async function runAllDays(): Promise<void> {
try {
const files = await readdir(import.meta.dir);
const files = await readdir("./src");
const fileNames = files
.map((file) => Number(file.replace(extname(file), "")))
.sort((a, b) => a - b);

fileNames.forEach(async (day) => {
if (Number.isNaN(day)) return;
const start = performance.now();
const result = await solveDay(day);
const end = performance.now();
console.log(`πŸŽ„ Day ${day} πŸŽ„ \x1b[90m[${(end - start).toFixed(
2
)} ms]\x1b[0m
Part 1: \x1b[32m${result.part1}\x1b[0m
Part 2: \x1b[31m${result.part2}\x1b[0m`);
});
const days: Record<number, { part1: string; part2: string; time: number }> =
{};

await Promise.all(
fileNames.map(async (day) => {
if (Number.isNaN(day)) return;
const start = performance.now();
const result = await solveDay(day);
const end = performance.now();

days[day] = { ...result, time: end - start };
})
);

Object.keys(days)
.map(Number)
.sort((a, b) => a - b)
.forEach((day) => {
const result = days[day];
console.log(`πŸŽ„ Day ${day} πŸŽ„ \x1b[90m[${result.time.toFixed(
2
)} ms]\x1b[0m
Part 1: \x1b[32m${result.part1}\x1b[0m
Part 2: \x1b[31m${result.part2}\x1b[0m`);
});
} catch (error) {
console.error("ERROR: ", error);
throw error;
Expand Down

0 comments on commit d9b10de

Please sign in to comment.