Skip to content

Commit a86014f

Browse files
feat(2023-03): validate cube game against limits
1 parent cff0f91 commit a86014f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

2023/day-02/game.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,27 @@ const parseGame = (gameString) => {
2323
}
2424
}
2525

26-
const validateGame = () => {
26+
const parseHex = (hex) => {
27+
return {
28+
r: parseInt(hex.substring(0, 2), 16),
29+
g: parseInt(hex.substring(2, 4), 16),
30+
b: parseInt(hex.substring(4, 6), 16)
31+
}
32+
}
33+
34+
const validateGame = (game, limit) => {
35+
const lim = parseHex(limit)
36+
37+
const tally = game.draws.reduce((acc, draw) => {
38+
const drawData = parseHex(draw)
39+
return {
40+
r: acc.r + drawData.r,
41+
g: acc.g + drawData.g,
42+
b: acc.b + drawData.b
43+
}
44+
}, { r: 0, g: 0, b: 0 })
2745

46+
return (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
2847
}
2948

3049
module.exports = {

0 commit comments

Comments
 (0)