-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
70 lines (53 loc) · 1.71 KB
/
solution.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
61
62
63
64
65
66
67
68
69
70
const fs = require('fs');
let input = fs.readFileSync('input.txt', 'utf8').trimEnd();
const gameConfigurations = input.split('\n');
function calculateGameScoreBasedOnRules() {
let totalScore = 0;
outer: for (const configuration of gameConfigurations) {
let [gamePoints, sets] = configuration.split(': ');
gamePoints = +gamePoints.match(/\d+/);
sets = sets.split('; ');
for (const set of sets) {
const individualCubes = set.split(', ');
const cubeTotals = {
red: 12,
green: 13,
blue: 14,
};
for (const individualCube of individualCubes) {
let [cubeValue, cubeColor] = individualCube.split(' ');
cubeValue = +cubeValue;
if (cubeValue > cubeTotals[cubeColor]) {
continue outer;
}
}
}
totalScore += gamePoints;
}
return totalScore;
}
function calculateAggregateMinimums() {
let totalAggregatedMinimum = 0;
for (const configuration of gameConfigurations) {
let [gamePoints, sets] = configuration.split(': ');
gamePoints = +gamePoints.match(/\d+/);
sets = sets.split('; ');
const minValues = {
red: 0,
green: 0,
blue: 0,
};
for (const set of sets) {
const individualCubes = set.split(', ');
for (const individualCube of individualCubes) {
let [cubeValue, cubeColor] = individualCube.split(' ');
cubeValue = +cubeValue;
minValues[cubeColor] = Math.max(minValues[cubeColor], cubeValue);
}
}
totalAggregatedMinimum += minValues.red * minValues.green * minValues.blue;
}
return totalAggregatedMinimum;
}
console.log('Part1: ', calculateGameScoreBasedOnRules());
console.log('Part2: ', calculateAggregateMinimums());