-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.js
58 lines (54 loc) · 1.46 KB
/
04.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
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 sum = 0;
rl.on("line", (word) => {
// regex for finding the results of each color per game
let redRegex = /\d* red/gm;
let blueRegex = /\d* blue/gm;
let greenRegex = /\d* green/gm;
// separate game into rounds
let rounds = word.split(";");
let results = [];
let powers = [0,0,0]
for (let round of rounds) {
// extract number of dice of each color per round
let roundResult = [
round.match(redRegex),
round.match(blueRegex),
round.match(greenRegex),
].map((res)=>{
if(res === null){
return 0
} else {
return Number.parseInt(res[0].split(" ")[0])
}
})
results.push(roundResult);
}
// in each round, check if the number of dice is the lowest
for(let r = 0; r< results.length;r++){
for(let c = 0; c < 3; c++){
// if we're at zero, set the color minimum value
if(results[r][c] > powers[c]){
powers[c] = results[r][c]
}
}
}
// sum powers
let setPower = 1
for(let i = 0; i < 3; i++){
setPower *= powers[i]
}
sum += setPower
});
rl.on("close", () => {
console.log(sum);
});
}
processLineByLine();