File tree 1 file changed +49
-0
lines changed
1 file changed +49
-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 play = ( time , record ) => {
10
+ const strategies = [ ] ;
11
+ for ( let i = 0 ; i < time ; i ++ ) {
12
+ const speed = i ;
13
+ const calculatedTime = ( time - speed ) * speed ;
14
+ if ( calculatedTime > record ) {
15
+ strategies . push ( speed ) ;
16
+ }
17
+ }
18
+
19
+ return strategies ;
20
+ } ;
21
+
22
+ const part1 = ( lines ) => {
23
+ const times = lines [ 0 ]
24
+ . split ( ":" ) [ 1 ]
25
+ . trim ( )
26
+ . split ( / + / )
27
+ . map ( ( num ) => Number . parseInt ( num . trim ( ) , 0 ) ) ;
28
+
29
+ const distances = lines [ 1 ]
30
+ . split ( ":" ) [ 1 ]
31
+ . trim ( )
32
+ . split ( / + / )
33
+ . map ( ( num ) => Number . parseInt ( num . trim ( ) , 0 ) ) ;
34
+
35
+ let result = 1 ;
36
+
37
+ for ( let i = 0 ; i < times . length ; i ++ ) {
38
+ const distance = distances [ i ] ;
39
+ const time = times [ i ] ;
40
+
41
+ const strategies = play ( time , distance ) ;
42
+
43
+ result *= strategies . length ;
44
+ }
45
+
46
+ return result ;
47
+ } ;
48
+
49
+ console . log ( part1 ( input ) ) ;
You can’t perform that action at this time.
0 commit comments