-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.js
60 lines (56 loc) · 1.45 KB
/
03.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const fs = require("fs");
const readline = require("readline");
async function processLineByLine() {
const fileStream = fs.createReadStream("input_for_3_4.txt");
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
let rules = {
red: 12,
green: 13,
blue: 14,
};
let sum = 0;
rl.on("line", (word) => {
let gameRegex = /Game \d*:/gm
let redRegex = /\d* red/gm
let blueRegex = /\d* blue/gm
let greenRegex = /\d* green/gm
let game = Number.parseInt(word.match(gameRegex)[0].split(" ")[1].replace(":",""))
let rounds = word.split(";")
let possible = true
for(let round of rounds){
let results = {
red: round.match(redRegex),
blue: round.match(blueRegex),
green: round.match(greenRegex)
}
if(results.red !== null){
if(Number.parseInt(results.red) > rules.red){
possible = false
break
}
}
if(results.green !== null){
if(Number.parseInt(results.green) > rules.green){
possible = false
break
}
}
if(results.blue !== null){
if(Number.parseInt(results.blue) > rules.blue){
possible = false
break
}
}
}
if(possible){
sum += game;
}
});
rl.on("close", () => {
console.log(sum);
});
}
processLineByLine();