File tree 2 files changed +56
-3
lines changed
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) => {
46
46
return ( tally . r <= lim . r && tally . g <= lim . g && tally . b <= lim . b )
47
47
}
48
48
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
+
49
56
module . exports = {
50
57
parseGame,
51
- validateGame
58
+ validateGame,
59
+ checksumGameSet
52
60
}
Original file line number Diff line number Diff line change 1
1
/* eslint-env mocha */
2
2
const { expect } = require ( 'chai' )
3
- const { parseGame, validateGame } = require ( './game' )
3
+ const { parseGame, validateGame, checksumGameSet } = require ( './game' )
4
4
5
5
describe ( '--- Day 2: Cube Conundrum ---' , ( ) => {
6
6
describe ( 'Part 1' , ( ) => {
@@ -58,7 +58,7 @@ describe('--- Day 2: Cube Conundrum ---', () => {
58
58
} )
59
59
60
60
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' , ( ) => {
62
62
const limits = '0c0d0e' // 12 red cubes, 13 green cubes, and 14 blue cubes
63
63
const data = [
64
64
{
@@ -103,5 +103,50 @@ describe('--- Day 2: Cube Conundrum ---', () => {
103
103
} )
104
104
} )
105
105
} )
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
+ } )
106
151
} )
107
152
} )
You can’t perform that action at this time.
0 commit comments