Skip to content

Commit 68485ba

Browse files
committed
Add tests and implementation for
cubeConundrumPartTwo
1 parent bc60cb0 commit 68485ba

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ describe("Day 02, cube conundrum", () => {
2828
expect(result).toEqual(2716);
2929
});
3030
});
31+
32+
describe("Part Two", () => {
33+
test("it should return 2286, example", () => {
34+
const result = cubeConundrumPartTwo(content_example);
35+
expect(result).toEqual(2286);
36+
});
37+
test("it should return 2716, input", () => {
38+
const result = cubeConundrumPartTwo(content_input);
39+
expect(result).toEqual(72227);
40+
});
41+
});
3142
});

src/day-02/cube-conundrum.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,40 @@ export function cubeConundrumPartOne(content: string): number {
3939
}, 0)
4040
);
4141
}
42+
43+
export function cubeConundrumPartTwo(content: string): number {
44+
// Split the content into lines
45+
const lines = content.trim().split("\n");
46+
47+
// Process each line
48+
return (
49+
lines
50+
.map((line) => {
51+
// Split the line into sets
52+
const sets = line.split(": ")[1].split("; ");
53+
54+
// find the highest count for each color
55+
const highestCount: maxCountType = sets.reduce(
56+
(acc: maxCountType, set: string) => {
57+
const pulls = set.split(", ");
58+
pulls.forEach((pull) => {
59+
const [count, color] = pull.split(" ");
60+
if (acc[color] < Number(count)) {
61+
acc[color] = Number(count);
62+
}
63+
});
64+
return acc;
65+
},
66+
{ red: 0, green: 0, blue: 0 }
67+
);
68+
69+
// multiply the results and return
70+
return Object.values(highestCount).reduce(
71+
(acc, count) => acc * count,
72+
1
73+
);
74+
})
75+
// Sum up the results
76+
.reduce((sum, result) => sum + result, 0)
77+
);
78+
}

0 commit comments

Comments
 (0)