Skip to content

Commit

Permalink
feat(2023-02): checksum a set of games
Browse files Browse the repository at this point in the history
produces a checksum of a set of games by
tallying the IDs of games that are valid
  • Loading branch information
amclin committed Dec 15, 2023
1 parent a86014f commit f69ddc5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
10 changes: 9 additions & 1 deletion 2023/day-02/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ const validateGame = (game, limit) => {
return (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
}

const checksumGameSet = (games, limit) => {
// tally the IDs of valid games
return games.reduce((acc, game) => {
return validateGame(game, limit) ? acc + game.id : acc
}, 0)
}

module.exports = {
parseGame,
validateGame
validateGame,
checksumGameSet
}
49 changes: 47 additions & 2 deletions 2023/day-02/game.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { parseGame, validateGame } = require('./game')
const { parseGame, validateGame, checksumGameSet } = require('./game')

describe('--- Day 2: Cube Conundrum ---', () => {
describe('Part 1', () => {
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('--- Day 2: Cube Conundrum ---', () => {
})

describe('validateGame', () => {
it.only('checks if the game is valid given the limits', () => {
it('checks if the game is valid given the limits', () => {
const limits = '0c0d0e' // 12 red cubes, 13 green cubes, and 14 blue cubes
const data = [
{
Expand Down Expand Up @@ -103,5 +103,50 @@ describe('--- Day 2: Cube Conundrum ---', () => {
})
})
})

describe('checksumGameSet', () => {
it('tallies the IDs of valid games', () => {
const limits = '0c0d0e' // 12 red cubes, 13 green cubes, and 14 blue cubes
const data = [
{
id: 1,
draws: [
'040003',
'010206',
'000200'
]
}, {
id: 2,
draws: [
'000201',
'010304',
'000101'
]
}, {
id: 3,
draws: [
'140806',
'040d05',
'010500'
]
}, {
id: 4,
draws: [
'030106',
'060300',
'0e030f'
]
}, {
id: 5,
draws: [
'060301',
'010202'
]
}
]

expect(checksumGameSet(data, limits)).to.equal(8)
})
})
})
})

0 comments on commit f69ddc5

Please sign in to comment.