Skip to content

Commit f69ddc5

Browse files
feat(2023-02): checksum a set of games
produces a checksum of a set of games by tallying the IDs of games that are valid
1 parent a86014f commit f69ddc5

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

2023/day-02/game.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ const validateGame = (game, limit) => {
4646
return (tally.r <= lim.r && tally.g <= lim.g && tally.b <= lim.b)
4747
}
4848

49+
const checksumGameSet = (games, limit) => {
50+
// tally the IDs of valid games
51+
return games.reduce((acc, game) => {
52+
return validateGame(game, limit) ? acc + game.id : acc
53+
}, 0)
54+
}
55+
4956
module.exports = {
5057
parseGame,
51-
validateGame
58+
validateGame,
59+
checksumGameSet
5260
}

2023/day-02/game.test.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { parseGame, validateGame } = require('./game')
3+
const { parseGame, validateGame, checksumGameSet } = require('./game')
44

55
describe('--- Day 2: Cube Conundrum ---', () => {
66
describe('Part 1', () => {
@@ -58,7 +58,7 @@ describe('--- Day 2: Cube Conundrum ---', () => {
5858
})
5959

6060
describe('validateGame', () => {
61-
it.only('checks if the game is valid given the limits', () => {
61+
it('checks if the game is valid given the limits', () => {
6262
const limits = '0c0d0e' // 12 red cubes, 13 green cubes, and 14 blue cubes
6363
const data = [
6464
{
@@ -103,5 +103,50 @@ describe('--- Day 2: Cube Conundrum ---', () => {
103103
})
104104
})
105105
})
106+
107+
describe('checksumGameSet', () => {
108+
it('tallies the IDs of valid games', () => {
109+
const limits = '0c0d0e' // 12 red cubes, 13 green cubes, and 14 blue cubes
110+
const data = [
111+
{
112+
id: 1,
113+
draws: [
114+
'040003',
115+
'010206',
116+
'000200'
117+
]
118+
}, {
119+
id: 2,
120+
draws: [
121+
'000201',
122+
'010304',
123+
'000101'
124+
]
125+
}, {
126+
id: 3,
127+
draws: [
128+
'140806',
129+
'040d05',
130+
'010500'
131+
]
132+
}, {
133+
id: 4,
134+
draws: [
135+
'030106',
136+
'060300',
137+
'0e030f'
138+
]
139+
}, {
140+
id: 5,
141+
draws: [
142+
'060301',
143+
'010202'
144+
]
145+
}
146+
]
147+
148+
expect(checksumGameSet(data, limits)).to.equal(8)
149+
})
150+
})
106151
})
107152
})

0 commit comments

Comments
 (0)