File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ const fs = require ( "node:fs" ) ;
2
+ const path = require ( "node:path" ) ;
3
+
4
+ const input = fs
5
+ . readFileSync ( path . join ( __dirname , "../input.txt" ) , "utf8" )
6
+ . split ( "\n" )
7
+ . filter ( ( line ) => line !== "" ) ;
8
+
9
+ const part1 = ( cards ) => {
10
+ const results = [ ] ;
11
+ for ( const card of cards ) {
12
+ const [ _ , winnindNumbersString , drawnNumbersString ] =
13
+ / C a r d + \d + : ( [ \d ] + ) + \| + ( [ \d ] + ) / . exec ( card ) ;
14
+ const winnindNumbers = winnindNumbersString
15
+ . trim ( )
16
+ . split ( " " )
17
+ . map ( ( num ) => Number . parseInt ( num , 10 ) )
18
+ . filter ( ( num ) => ! isNaN ( num ) ) ;
19
+
20
+ const drawnNumbers = drawnNumbersString
21
+ . trim ( )
22
+ . split ( " " )
23
+ . map ( ( num ) => Number . parseInt ( num , 10 ) )
24
+ . filter ( ( num ) => ! isNaN ( num ) ) ;
25
+
26
+ let matches = [ ] ;
27
+
28
+ for ( const num of drawnNumbers ) {
29
+ if ( winnindNumbers . includes ( num ) ) {
30
+ matches . push ( num ) ;
31
+ }
32
+ }
33
+
34
+ matches . length > 0 && results . push ( 2 ** ( matches . length - 1 ) ) ;
35
+ }
36
+
37
+ return results . reduce ( ( sum , curr ) => sum + curr , 0 ) ;
38
+ } ;
39
+
40
+ console . log ( part1 ( input ) ) ;
You can’t perform that action at this time.
0 commit comments