final test for the setup. aoc 2021 / 2
This commit is contained in:
parent
888cd821ac
commit
7fd9576422
22
aoc_2021/day-02/example.test.ts
Normal file
22
aoc_2021/day-02/example.test.ts
Normal 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);
|
||||
});
|
0
aoc_2021/day-02/logs/.gitkeep
Normal file
0
aoc_2021/day-02/logs/.gitkeep
Normal file
7
aoc_2021/day-02/logs/out_1.txt
Normal file
7
aoc_2021/day-02/logs/out_1.txt
Normal file
@ -0,0 +1,7 @@
|
||||
=== COMPUTATION ===
|
||||
|
||||
|
||||
=== /COMPUTATION ===
|
||||
|
||||
=== ANSWER TO P1 ===
|
||||
1480518
|
7
aoc_2021/day-02/logs/out_2.txt
Normal file
7
aoc_2021/day-02/logs/out_2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
=== COMPUTATION ===
|
||||
|
||||
|
||||
=== /COMPUTATION ===
|
||||
|
||||
=== ANSWER TO P2 ===
|
||||
1282809906
|
41
aoc_2021/day-02/part_1.ts
Normal file
41
aoc_2021/day-02/part_1.ts
Normal 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
42
aoc_2021/day-02/part_2.ts
Normal 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
1000
aoc_2021/day-02/problem.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user