updates and test w/ day 01 2021

This commit is contained in:
Lizzy Hunt 2023-12-01 16:05:44 -07:00
parent cdb12dbc28
commit 888cd821ac
No known key found for this signature in database
GPG Key ID: E835BD4B08CCAF96
14 changed files with 145 additions and 22 deletions

4
aoc
View File

@ -16,7 +16,7 @@ else
fi fi
exec_test() { exec_test() {
bun test bun test "example.test.ts"
} }
exec_part() { exec_part() {
@ -100,6 +100,7 @@ aoc() {
mkdir -p $curr mkdir -p $curr
cp -r template/* $curr cp -r template/* $curr
cd $curr
get_problem_input $(get_aoc_cookie) $year $day $AOCINPUT get_problem_input $(get_aoc_cookie) $year $day $AOCINPUT
echo "Initialized $curr" echo "Initialized $curr"
@ -118,6 +119,7 @@ aoc() {
aoc init $year $day aoc init $year $day
fi fi
cd $curr
exec_test exec_test
fi fi

View File

@ -0,0 +1,26 @@
import { expect, test } from "bun:test";
import { main as part1 } from "./part_1";
import { main as part2 } from "./part_2";
const example = `199
200
208
210
200
207
240
269
260
263`.split("\n");
test("part1", async () => {
const answer = 7;
const res = await part1(example);
expect(res).toEqual(answer);
});
test("part2", async () => {
const answer = 5;
const res = await part2(example);
expect(res).toEqual(answer);
});

View File

View File

@ -0,0 +1,7 @@
=== COMPUTATION ===
=== /COMPUTATION ===
=== ANSWER TO P1 ===
1713

View File

@ -0,0 +1,7 @@
=== COMPUTATION ===
=== /COMPUTATION ===
=== ANSWER TO P2 ===
1734

26
aoc_2021/day-01/part_1.ts Normal file
View File

@ -0,0 +1,26 @@
export const main = async (lines: string[]): Promise<number | string> => {
return lines
.map((x) => Number(x))
.map((num, i, arr) => {
if (i > 0 && arr[i - 1] < num) {
return true;
}
})
.filter((x) => x).length;
};
const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
if (isrun) {
const file = Bun.file("./problem.txt");
const text = await file.text();
const lines = text.split("\n");
console.log("=== COMPUTATION ===\n");
const answer = await main(lines);
console.log("\n=== /COMPUTATION ===\n");
console.log("=== ANSWER TO P1 ===");
console.log(answer);
}

30
aoc_2021/day-01/part_2.ts Normal file
View File

@ -0,0 +1,30 @@
export const main = async (lines: string[]): Promise<number | string> => {
return lines
.map((x) => Number(x))
.map((_num, i, arr) => {
if (i > arr.length - 3) return -1;
return arr.slice(i, i + 3).reduce((acc, x) => acc + x, 0);
})
.map((num, i, arr) => {
return i > 0 && arr[i - 1] < num;
})
.filter((x) => x).length;
};
//
const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
if (isrun) {
const file = Bun.file("./problem.txt");
const text = await file.text();
const lines = text.split("\n");
console.log("=== COMPUTATION ===\n");
const answer = await main(lines);
console.log("\n=== /COMPUTATION ===\n");
console.log("=== ANSWER TO P2 ===");
console.log(answer);
}

BIN
bun.lockb Executable file

Binary file not shown.

9
package.json Normal file
View File

@ -0,0 +1,9 @@
{
"name": "aoc",
"devDependencies": {
"bun-types": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}

View File

@ -2,7 +2,7 @@ import { expect, test } from "bun:test";
import { main as part1 } from "./part_1"; import { main as part1 } from "./part_1";
import { main as part2 } from "./part_2"; import { main as part2 } from "./part_2";
// const example = ``; // const example = ``.split("\n");;
const example = `1 2 3 4 5`.split(" "); const example = `1 2 3 4 5`.split(" ");
test("part1", async () => { test("part1", async () => {
@ -11,8 +11,8 @@ test("part1", async () => {
expect(res).toEqual(answer); expect(res).toEqual(answer);
}); });
test("part2", async () => { //test("part2", async () => {
const answer = 5 + 5; // const answer = 5 + 5;
const res = await part2(example); // const res = await part2(example);
expect(res).toEqual(answer); // expect(res).toEqual(answer);
}); //});

View File

@ -1,6 +1,4 @@
export const main = async ( export const main = async (lines: string[]): Promise<number | string> => {
lines: string[]
): Promise<number | string> => {
const answer = lines.length; const answer = lines.length;
// delete me! // delete me!
@ -9,6 +7,8 @@ export const main = async (
return answer; return answer;
}; };
//
const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path; const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
if (isrun) { if (isrun) {
const file = Bun.file("./problem.txt"); const file = Bun.file("./problem.txt");

View File

@ -1,14 +1,9 @@
export const main = async ( export const main = async (_lines: string[]): Promise<number | string> => {
lines: string[] return 10;
): Promise<number | string> => {
const answer = lines.length;
// delete me!
console.log(lines);
return answer + 5;
}; };
//
const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path; const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path;
if (isrun) { if (isrun) {
const file = Bun.file("./problem.txt"); const file = Bun.file("./problem.txt");
@ -24,4 +19,3 @@ if (isrun) {
console.log("=== ANSWER TO P2 ==="); console.log("=== ANSWER TO P2 ===");
console.log(answer); console.log(answer);
} }

22
tsconfig.json Normal file
View File

@ -0,0 +1,22 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"allowImportingTsExtensions": true,
"noEmit": true,
"composite": true,
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"types": [
"bun-types" // add Bun global
]
}
}