Skip to content

Commit

Permalink
feat(2023-03): validate cube game against limits
Browse files Browse the repository at this point in the history
  • Loading branch information
amclin committed Dec 15, 2023
1 parent cff0f91 commit a86014f
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion 2023/day-02/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,27 @@ const parseGame = (gameString) => {
}
}

const validateGame = () => {
const parseHex = (hex) => {
return {
r: parseInt(hex.substring(0, 2), 16),
g: parseInt(hex.substring(2, 4), 16),
b: parseInt(hex.substring(4, 6), 16)
}
}

const validateGame = (game, limit) => {
const lim = parseHex(limit)

const tally = game.draws.reduce((acc, draw) => {
const drawData = parseHex(draw)
return {
r: acc.r + drawData.r,
g: acc.g + drawData.g,
b: acc.b + drawData.b
}
}, { r: 0, g: 0, b: 0 })

return (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
}

module.exports = {
Expand Down

0 comments on commit a86014f

Please sign in to comment.