Skip to content

Commit bc60cb0

Browse files
committed
Add cube conundrum tests and implementation
1 parent e17eccf commit bc60cb0

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/day-02/cube-conundrum.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { cubeConundrumPartOne, cubeConundrumPartTwo } from "./cube-conundrum";
3+
4+
const example = Bun.file("src/day-02/example.txt");
5+
const input = Bun.file("src/day-02/input.txt");
6+
7+
const content_input = await input.text();
8+
const content_example = await example.text();
9+
10+
type maxCountType = {
11+
[key: string]: number;
12+
};
13+
14+
const maxCount: maxCountType = {
15+
red: 12,
16+
green: 13,
17+
blue: 14,
18+
};
19+
20+
describe("Day 02, cube conundrum", () => {
21+
describe("Part One", () => {
22+
test("it should return 8, example", () => {
23+
const result = cubeConundrumPartOne(content_example);
24+
expect(result).toEqual(8);
25+
});
26+
test("it should return 2716, input", () => {
27+
const result = cubeConundrumPartOne(content_input);
28+
expect(result).toEqual(2716);
29+
});
30+
});
31+
});

src/day-02/cube-conundrum.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
type maxCountType = {
2+
[key: string]: number;
3+
};
4+
5+
const maxCount: maxCountType = {
6+
red: 12,
7+
green: 13,
8+
blue: 14,
9+
};
10+
11+
export function cubeConundrumPartOne(content: string): number {
12+
// Split the content into lines
13+
const lines = content.trim().split("\n");
14+
15+
// Process each line
16+
return (
17+
lines
18+
.map((line) => {
19+
// Split the line into sets
20+
const sets = line.split(": ")[1].split("; ");
21+
22+
// Check if all pulls in a set are valid
23+
const areAllPullsValid = sets.map((set) => {
24+
const pulls = set.split(", ");
25+
26+
// Check if a pull is valid
27+
return pulls.every((pull) => {
28+
const [count, color] = pull.split(" ");
29+
return maxCount[color] >= Number(count);
30+
});
31+
});
32+
33+
// Check if all sets are valid
34+
return areAllPullsValid.every((p) => p);
35+
})
36+
// Sum up the results
37+
.reduce((sum, result, index) => {
38+
return result ? sum + (index + 1) : sum;
39+
}, 0)
40+
);
41+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy