File tree Expand file tree Collapse file tree 3 files changed +83
-16
lines changed Expand file tree Collapse file tree 3 files changed +83
-16
lines changed Original file line number Diff line number Diff line change
1
+ const { spawn } = require ( 'child_process' ) ;
1
2
const fs = require ( 'fs' ) ;
3
+ const { format } = require ( 'path' ) ;
4
+ const { nextTick } = require ( 'process' ) ;
2
5
const readline = require ( 'readline' ) ;
3
6
4
7
async function processLineByLine ( ) {
5
8
const fileStream = fs . createReadStream ( '01\\input.txt' ) ;
9
+ //const fileStream = fs.createReadStream('01\\testinput.txt');
6
10
7
11
const rl = readline . createInterface ( {
8
12
input : fileStream ,
9
13
crlfDelay : Infinity
10
14
} ) ;
11
15
12
- let count = 0 ;
13
- let up = 0 ;
14
- let down = 0 ;
15
- let before = 0 ;
16
- let first = true ;
17
-
16
+ let biggest = 0 ;
17
+ let amount = 0 ;
18
18
for await ( const line of rl ) {
19
- if ( ! first ) {
20
- if ( Number ( line ) > Number ( before ) ) up ++ ;
21
- if ( Number ( before ) > Number ( line ) ) down ++ ;
22
- } else {
23
- first = false ;
19
+ amount = amount + Number ( line ) ;
20
+ //console.log(`line = ${line}; biggest=${biggest}; amount=${amount}`)
21
+ if ( ! isNumeric ( line ) ) {
22
+ if ( biggest < amount ) {
23
+ biggest = amount ;
24
+ }
25
+ amount = 0 ;
24
26
}
25
- before = line
26
- count ++ ;
27
27
}
28
- console . log ( `Ups: ${ up } Downs: ${ down } ` ) ;
29
- console . log ( count ) ;
28
+ if ( biggest < amount ) {
29
+ biggest = amount ;
30
+ }
31
+ amount = 0 ;
32
+
33
+ console . log ( `Result: ${ biggest } ` ) ;
34
+
30
35
}
31
36
32
- processLineByLine ( ) ;
37
+ function isNumeric ( value ) {
38
+ return / ^ - ? \d + $ / . test ( value ) ;
39
+ }
40
+
41
+
42
+ processLineByLine ( ) ;
Original file line number Diff line number Diff line change
1
+ const { spawn } = require ( 'child_process' ) ;
2
+ const fs = require ( 'fs' ) ;
3
+ const { format } = require ( 'path' ) ;
4
+ const { nextTick } = require ( 'process' ) ;
5
+ const readline = require ( 'readline' ) ;
6
+
7
+ async function processLineByLine ( ) {
8
+ //var array = fs.readFileSync('01\\testinput.txt').toString().split("\n");
9
+ var array = fs . readFileSync ( '01\\input.txt' ) . toString ( ) . split ( "\n" ) ;
10
+
11
+ let dwarf = 0 ;
12
+ let amount = 0 ;
13
+ let result = new Array ( ) ;
14
+
15
+ array . forEach ( ( item ) => {
16
+ console . log ( item ) ;
17
+ if ( isNumeric ( item ) ) {
18
+ amount = amount + Number ( item ) ;
19
+ } else {
20
+ result . push ( amount ) ;
21
+ amount = 0 ;
22
+ }
23
+ console . log ( `Result: ${ result } ` ) ;
24
+ } ) ;
25
+ if ( amount != 0 ) result . push ( amount ) ;
26
+
27
+ result . sort ( function ( a , b ) {
28
+ return b - a ;
29
+ } ) ;
30
+
31
+ console . log ( `Result: ${ result [ 0 ] + result [ 1 ] + result [ 2 ] } ` ) ;
32
+
33
+ }
34
+
35
+ //this is why js is kind a hell
36
+ function isNumeric ( str ) {
37
+ if ( typeof str != "string" ) return false // we only process strings!
38
+ return ! isNaN ( str ) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
39
+ ! isNaN ( parseFloat ( str ) ) // ...and ensure strings of whitespace fail
40
+ }
41
+
42
+
43
+ processLineByLine ( ) ;
Original file line number Diff line number Diff line change
1
+ 1000
2
+ 2000
3
+ 3000
4
+
5
+ 4000
6
+
7
+ 5000
8
+ 6000
9
+
10
+ 7000
11
+ 8000
12
+ 9000
13
+
14
+ 10000
You can’t perform that action at this time.
0 commit comments