File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Filter options
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Original file line number Diff line number Diff line change
1
+ export const part1 = ( { input } ) => {
2
+ let result = 0 ;
3
+ const constraints = { red : 12 , green : 13 , blue : 14 } ;
4
+
5
+ input . forEach ( ( row ) => {
6
+ let success = true ;
7
+ const { hands, gameNumber } = splitInput ( { row } ) ;
8
+
9
+ hands . forEach ( ( hand ) => {
10
+ [ "red" , "green" , "blue" ] . forEach ( ( color ) => {
11
+ if ( hand . hasOwnProperty ( color ) && hand [ color ] > constraints [ color ] ) {
12
+ success = false ;
13
+ }
14
+ } ) ;
15
+ } ) ;
16
+
17
+ if ( success ) {
18
+ result += gameNumber ;
19
+ }
20
+ } ) ;
21
+
22
+ return result ;
23
+ } ;
24
+
25
+ export const part2 = ( { input } ) => {
26
+ const result = 0 ;
27
+ return result ;
28
+ } ;
29
+
30
+ const splitInput = ( { row } ) => {
31
+ const game = row . split ( ": " ) [ 0 ] ;
32
+ const gameNumber = parseInt ( game . split ( " " ) [ 1 ] ) ;
33
+ let hands = row . substring ( game . length + 2 ) . split ( "; " ) ;
34
+ hands = hands . map ( ( hand ) => {
35
+ let splitHands = hand . split ( ", " ) ;
36
+ return splitHands . reduce ( ( acc , nums ) => {
37
+ const [ count , color ] = nums . split ( " " ) ;
38
+ acc [ color ] = Number ( count ) ;
39
+ return acc ;
40
+ } , { } ) ;
41
+ } ) ;
42
+ return { gameNumber, hands } ;
43
+ } ;
You can’t perform that action at this time.
0 commit comments