Skip to content

Commit 3308a61

Browse files
committed
01 - solution
1 parent 3122915 commit 3308a61

File tree

3 files changed

+83
-16
lines changed

3 files changed

+83
-16
lines changed

01/01.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
1+
const { spawn } = require('child_process');
12
const fs = require('fs');
3+
const { format } = require('path');
4+
const { nextTick } = require('process');
25
const readline = require('readline');
36

47
async function processLineByLine() {
58
const fileStream = fs.createReadStream('01\\input.txt');
9+
//const fileStream = fs.createReadStream('01\\testinput.txt');
610

711
const rl = readline.createInterface({
812
input: fileStream,
913
crlfDelay: Infinity
1014
});
1115

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;
1818
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;
2426
}
25-
before = line
26-
count++;
2727
}
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+
3035
}
3136

32-
processLineByLine();
37+
function isNumeric(value) {
38+
return /^-?\d+$/.test(value);
39+
}
40+
41+
42+
processLineByLine();

01/02.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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();

01/testinput.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1000
2+
2000
3+
3000
4+
5+
4000
6+
7+
5000
8+
6000
9+
10+
7000
11+
8000
12+
9000
13+
14+
10000

0 commit comments

Comments
 (0)