summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xaoc4
-rw-r--r--aoc_2021/day-01/example.test.ts26
-rw-r--r--aoc_2021/day-01/logs/.gitkeep0
-rw-r--r--aoc_2021/day-01/logs/out_1.txt7
-rw-r--r--aoc_2021/day-01/logs/out_2.txt7
-rw-r--r--aoc_2021/day-01/part_1.ts26
-rw-r--r--aoc_2021/day-01/part_2.ts30
-rw-r--r--aoc_2021/day-01/problem.txt (renamed from problem.txt)0
-rwxr-xr-xbun.lockbbin0 -> 1678 bytes
-rw-r--r--package.json9
-rw-r--r--template/example.test.ts12
-rw-r--r--template/part_1.ts8
-rw-r--r--template/part_2.ts16
-rw-r--r--tsconfig.json22
14 files changed, 145 insertions, 22 deletions
diff --git a/aoc b/aoc
index 4cda9ed..51e6da8 100755
--- a/aoc
+++ b/aoc
@@ -16,7 +16,7 @@ else
fi
exec_test() {
- bun test
+ bun test "example.test.ts"
}
exec_part() {
@@ -100,6 +100,7 @@ aoc() {
mkdir -p $curr
cp -r template/* $curr
+ cd $curr
get_problem_input $(get_aoc_cookie) $year $day $AOCINPUT
echo "Initialized $curr"
@@ -118,6 +119,7 @@ aoc() {
aoc init $year $day
fi
+ cd $curr
exec_test
fi
diff --git a/aoc_2021/day-01/example.test.ts b/aoc_2021/day-01/example.test.ts
new file mode 100644
index 0000000..329dd72
--- /dev/null
+++ b/aoc_2021/day-01/example.test.ts
@@ -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);
+});
diff --git a/aoc_2021/day-01/logs/.gitkeep b/aoc_2021/day-01/logs/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/aoc_2021/day-01/logs/.gitkeep
diff --git a/aoc_2021/day-01/logs/out_1.txt b/aoc_2021/day-01/logs/out_1.txt
new file mode 100644
index 0000000..fb45427
--- /dev/null
+++ b/aoc_2021/day-01/logs/out_1.txt
@@ -0,0 +1,7 @@
+=== COMPUTATION ===
+
+
+=== /COMPUTATION ===
+
+=== ANSWER TO P1 ===
+1713
diff --git a/aoc_2021/day-01/logs/out_2.txt b/aoc_2021/day-01/logs/out_2.txt
new file mode 100644
index 0000000..d95824d
--- /dev/null
+++ b/aoc_2021/day-01/logs/out_2.txt
@@ -0,0 +1,7 @@
+=== COMPUTATION ===
+
+
+=== /COMPUTATION ===
+
+=== ANSWER TO P2 ===
+1734
diff --git a/aoc_2021/day-01/part_1.ts b/aoc_2021/day-01/part_1.ts
new file mode 100644
index 0000000..705d353
--- /dev/null
+++ b/aoc_2021/day-01/part_1.ts
@@ -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);
+}
diff --git a/aoc_2021/day-01/part_2.ts b/aoc_2021/day-01/part_2.ts
new file mode 100644
index 0000000..492b68c
--- /dev/null
+++ b/aoc_2021/day-01/part_2.ts
@@ -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);
+}
diff --git a/problem.txt b/aoc_2021/day-01/problem.txt
index 97c1adf..97c1adf 100644
--- a/problem.txt
+++ b/aoc_2021/day-01/problem.txt
diff --git a/bun.lockb b/bun.lockb
new file mode 100755
index 0000000..91ed993
--- /dev/null
+++ b/bun.lockb
Binary files differ
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..08ba2f4
--- /dev/null
+++ b/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "aoc",
+ "devDependencies": {
+ "bun-types": "latest"
+ },
+ "peerDependencies": {
+ "typescript": "^5.0.0"
+ }
+}
diff --git a/template/example.test.ts b/template/example.test.ts
index f3e80eb..a0522cc 100644
--- a/template/example.test.ts
+++ b/template/example.test.ts
@@ -2,7 +2,7 @@ import { expect, test } from "bun:test";
import { main as part1 } from "./part_1";
import { main as part2 } from "./part_2";
-// const example = ``;
+// const example = ``.split("\n");;
const example = `1 2 3 4 5`.split(" ");
test("part1", async () => {
@@ -11,8 +11,8 @@ test("part1", async () => {
expect(res).toEqual(answer);
});
-test("part2", async () => {
- const answer = 5 + 5;
- const res = await part2(example);
- expect(res).toEqual(answer);
-});
+//test("part2", async () => {
+// const answer = 5 + 5;
+// const res = await part2(example);
+// expect(res).toEqual(answer);
+//});
diff --git a/template/part_1.ts b/template/part_1.ts
index f43fbd6..ab01f78 100644
--- a/template/part_1.ts
+++ b/template/part_1.ts
@@ -1,6 +1,4 @@
-export const main = async (
- lines: string[]
-): Promise<number | string> => {
+export const main = async (lines: string[]): Promise<number | string> => {
const answer = lines.length;
// delete me!
@@ -9,12 +7,14 @@ export const main = async (
return answer;
};
+//
+
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);
diff --git a/template/part_2.ts b/template/part_2.ts
index cdf4590..6cd6924 100644
--- a/template/part_2.ts
+++ b/template/part_2.ts
@@ -1,20 +1,15 @@
-export const main = async (
- lines: string[]
-): Promise<number | string> => {
- const answer = lines.length;
-
- // delete me!
- console.log(lines);
-
- return answer + 5;
+export const main = async (_lines: string[]): Promise<number | string> => {
+ return 10;
};
+//
+
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);
@@ -24,4 +19,3 @@ if (isrun) {
console.log("=== ANSWER TO P2 ===");
console.log(answer);
}
-
diff --git a/tsconfig.json b/tsconfig.json
new file mode 100644
index 0000000..7556e1d
--- /dev/null
+++ b/tsconfig.json
@@ -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
+ ]
+ }
+}