-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ts
33 lines (28 loc) · 884 Bytes
/
part2.ts
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
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n');
let result = 0;
for (const line of input) {
const id = Number(line.split(': ')[0].split('Game ')[1]);
const game = line
.split(': ')[1]
.split('; ')
.map((x) => {
const result = {
red: 0,
green: 0,
blue: 0,
};
const objects = x.split(', ');
for (const object of objects) {
if (object.endsWith('red')) result.red = Number(object.split(' ')[0]);
if (object.endsWith('green')) result.green = Number(object.split(' ')[0]);
if (object.endsWith('blue')) result.blue = Number(object.split(' ')[0]);
}
return result;
});
const redMax = Math.max(...game.map((x) => x.red));
const greenMax = Math.max(...game.map((x) => x.green));
const blueMax = Math.max(...game.map((x) => x.blue));
result += redMax * greenMax * blueMax;
}
console.log(result);