From 888cd821acc7582255320e0ddbddd2495e505cd4 Mon Sep 17 00:00:00 2001 From: Lizzy Hunt Date: Fri, 1 Dec 2023 16:05:44 -0700 Subject: [PATCH] updates and test w/ day 01 2021 --- aoc | 4 ++- aoc_2021/day-01/example.test.ts | 26 ++++++++++++++++++ aoc_2021/day-01/logs/.gitkeep | 0 aoc_2021/day-01/logs/out_1.txt | 7 +++++ aoc_2021/day-01/logs/out_2.txt | 7 +++++ aoc_2021/day-01/part_1.ts | 26 ++++++++++++++++++ aoc_2021/day-01/part_2.ts | 30 +++++++++++++++++++++ problem.txt => aoc_2021/day-01/problem.txt | 0 bun.lockb | Bin 0 -> 1678 bytes package.json | 9 +++++++ template/example.test.ts | 12 ++++----- template/part_1.ts | 8 +++--- template/part_2.ts | 16 ++++------- tsconfig.json | 22 +++++++++++++++ 14 files changed, 145 insertions(+), 22 deletions(-) create mode 100644 aoc_2021/day-01/example.test.ts create mode 100644 aoc_2021/day-01/logs/.gitkeep create mode 100644 aoc_2021/day-01/logs/out_1.txt create mode 100644 aoc_2021/day-01/logs/out_2.txt create mode 100644 aoc_2021/day-01/part_1.ts create mode 100644 aoc_2021/day-01/part_2.ts rename problem.txt => aoc_2021/day-01/problem.txt (100%) create mode 100755 bun.lockb create mode 100644 package.json create mode 100644 tsconfig.json 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 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 => { + 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 => { + 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 similarity index 100% rename from problem.txt rename to aoc_2021/day-01/problem.txt diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..91ed9930ac76514562cb8ebdfdea2d36fa2ab5fd GIT binary patch literal 1678 zcmY#Z)GsYA(of3F(@)JSQ%EY!;{sycoc!eMw9K4T-L(9o+{6;yG6OCq1_lNllREtk zQ|y_iM<4O})HyF&hvok#?c;0CNw7YzbF}mf{l^Ma1O&_wih%=-Zh-R3VG0-$^OM11 zTtJeOfdQ-l$ZqggD>mxMZ}?IDCjD%O&}*gnstr2Yt*J?jP!q_dK)wJ0VIZ~!+5 z4=hTUfa#9{DzyRX2YDFe8JIX>6Ci3~>R~iUABYX(!)OMEfBgSojwHr#BGoWL%|$kv zrDbcpar%?bBAr!j_ji0;H1+pK;~Csre75G*T~w2Ld$@Xs<$^7f)Te7Uy7~rQ`D9k! zW-oE#(+LqxkD&GxQLQteuz;D1a6bbB%d7Z_C+_TCz<9V`V28L#XR&?$Jd;z$HofG@ z^SK$l!=ohBjBn$)WtlIw3Ax{SyprYJ2UGj{IR!`E9e7?!uW9|(NTi=Y%2=RkVKfO^ z;YyLi1TIKj#sO#sxB{iQY>JDL^)mB{OA>Q(^kBJ9FQh0n)lR|4K%qFZDm6b%N5MoP zF()%UFFy^GUtr+he+U5a!Ql?|JO`BKvMDt-vIFYHVG78<7ErYgMCt+A1JYLjRlfwT zFWSf~w$uXXYBRWfhN!LvI|W08^%h9hmlQ+Fn*+Z^mKIH!AOb4*Kw|v& z7Tdg&E!R hSCU=@aV^A!c#J_<3N#17(KXdG)-wW`1N79O1OQ3>BiR4| literal 0 HcmV?d00001 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 => { +export const main = async (lines: string[]): Promise => { 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 => { - const answer = lines.length; - - // delete me! - console.log(lines); - - return answer + 5; +export const main = async (_lines: string[]): Promise => { + 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 + ] + } +}