Skip to content

Commit

Permalink
feat: add script to create new day files with templates for solution …
Browse files Browse the repository at this point in the history
…and tests
  • Loading branch information
wiktoriavh committed Dec 5, 2024
1 parent 451b121 commit 34c3059
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
34 changes: 34 additions & 0 deletions script/newDay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { promises as fs } from "fs";
import path from "path";

const createNewDay = async (day: number) => {
const dayString = day.toString().padStart(2, "0");
const inputPath = `./inputs/${dayString}.txt`;
const solutionPath = `./src/${dayString}.ts`;
const testPath = `./test/${dayString}.test.ts`;

const templateDayPath = path.resolve(__dirname, "./template/day.txt");
const templateDayContent = await fs.readFile(templateDayPath, "utf-8");
await fs.writeFile(solutionPath, templateDayContent);

const templateTestPath = path.resolve(__dirname, "./template/test.txt");
const templateTestContent = await fs.readFile(templateTestPath, "utf-8");
const updatedTestContent = templateTestContent.replace(/{{day}}/g, dayString);
await fs.writeFile(testPath, updatedTestContent);

await fs.writeFile(inputPath, "");
};

const dayArg = process.argv[2];
if (!dayArg) {
console.error("Please provide a day number as an argument.");
process.exit(1);
}

const day = parseInt(dayArg, 10);
if (isNaN(day) || day < 1 || day > 25) {
console.error("Please provide a valid day number between 1 and 25.");
process.exit(1);
}

createNewDay(day);
13 changes: 13 additions & 0 deletions script/template/day.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function solution(input: string): { part1: string; part2: string } {
let result1 = 0;
let result2 = 0;

const lines = input.split("\n");

return {
part1: result1.toString(),
part2: result2.toString(),
};
}

export default solution;
20 changes: 20 additions & 0 deletions script/template/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test, describe } from "bun:test";
import solution from "../src/{{day}}";

const EXAMPLE_1 = `1 2 3 4`;
const RESULT_1 = "2";

const EXAMPLE_2 = EXAMPLE_1;
const RESULT_2 = "31";

describe("Day 2", () => {
test("Part 1", () => {
const result = solution(EXAMPLE_1);
expect(result.part1).toBe(RESULT_1);
});

test.skip("Part 2", () => {
const result = solution(EXAMPLE_2);
expect(result.part2).toBe(RESULT_2);
});
});

0 comments on commit 34c3059

Please sign in to comment.