final test for the setup. aoc 2021 / 2

This commit is contained in:
Lizzy Hunt 2023-12-01 16:13:41 -07:00
parent 888cd821ac
commit 7fd9576422
No known key found for this signature in database
GPG Key ID: E835BD4B08CCAF96
7 changed files with 1119 additions and 0 deletions

View File

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

View File

View File

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

View File

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

41
aoc_2021/day-02/part_1.ts Normal file
View File

@ -0,0 +1,41 @@
export const main = async (lines: string[]): Promise<number | string> => {
const { depth, horiz } = lines
.map((line) => line.split(" "))
.reduce(
(acc, [dir, delta]) => {
const d = Number(delta);
if (dir === "forward") {
acc.horiz += d;
}
if (dir === "up") {
acc.depth -= d;
}
if (dir === "down") {
acc.depth += d;
}
return acc;
},
{ horiz: 0, depth: 0 },
);
return depth * horiz;
};
//
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);
}

42
aoc_2021/day-02/part_2.ts Normal file
View File

@ -0,0 +1,42 @@
export const main = async (lines: string[]): Promise<number | string> => {
const { depth, horiz } = lines
.map((line) => line.split(" "))
.reduce(
(acc, [dir, delta]) => {
const d = Number(delta);
if (dir === "forward") {
acc.horiz += d;
acc.depth += acc.aim * d;
}
if (dir === "up") {
acc.aim -= d;
}
if (dir === "down") {
acc.aim += d;
}
return acc;
},
{ horiz: 0, depth: 0, aim: 0 },
);
return depth * horiz;
};
//
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);
}

1000
aoc_2021/day-02/problem.txt Normal file

File diff suppressed because it is too large Load Diff