File tree Expand file tree Collapse file tree 2 files changed +56
-3
lines changed
Expand file tree Collapse file tree 2 files changed +56
-3
lines changed Original file line number Diff line number Diff 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+
4956module . exports = {
5057 parseGame,
51- validateGame
58+ validateGame,
59+ checksumGameSet
5260}
Original file line number Diff line number Diff line change 11/* eslint-env mocha */
22const { expect } = require ( 'chai' )
3- const { parseGame, validateGame } = require ( './game' )
3+ const { parseGame, validateGame, checksumGameSet } = require ( './game' )
44
55describe ( '--- 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} )
You can’t perform that action at this time.
0 commit comments