Day 2: Cube Conundrum
Already feeling more used to this kind of problem solving after day 1. Writing this in retrospect but I think this was fairly smooth and easy.
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const redLimit = 12;
const greenLimit = 13;
const blueLimit = 14;
const possibleGameIDs = [];
const outcomePowers = [];
// console.log(input);
const gameStrs = input.split('\n');
// console.log(gameStrings);
const allGameOutcomes = [];
// set up to convert strings to objects we can use
gameStrs.forEach(function(gameStr) {
const gameOutcomeStrs = [];
// console.log(gameStr);
const splitGameStr = gameStr.split(':');
// console.log(splitGameStr);
const cleanGameStr = splitGameStr[1];
// console.log(cleanGameStr);
const roundStrs = cleanGameStr.split(';');
// console.log('roundStrs',roundStrs);
roundStrs.forEach(function(rndStr) {
const outcomeStrs = rndStr.split(',');
// console.log('outcomeStrs', outcomeStrs);
outcomeStrs.forEach(function(outcomeStr){
const cleanOutcomeStr = outcomeStr.trim();
const splitOutcomeStr = cleanOutcomeStr.split(' ');
// console.log(splitOutcomeStr);
const colour = splitOutcomeStr[1];
const num = parseFloat(splitOutcomeStr[0]);
const outcome = [colour, num];
gameOutcomeStrs.push(outcome);
});
});
// console.log('gameOutcomeStrs',gameOutcomeStrs);
allGameOutcomes.push(gameOutcomeStrs);
});
// console.log('allGameOutcomes',allGameOutcomes);
allGameOutcomes.forEach(function(outcomeSet, index) {
const gameID = index + 1;
// console.log('gameID',gameID);
var gameIsPossible = true;
var highestRedOutcome = 0;
var highestGreenOutcome = 0;
var highestBlueOutcome = 0;
outcomeSet.forEach(function(outcome) {
// console.log('outcome',outcome);
// console.log('outcome[0]',outcome[0]);
// console.log('outcome[1]',outcome[1]);
// console.log('blueLimit',blueLimit);
if( outcome[0] === 'red' ) {
if( outcome[1] > redLimit ) {
gameIsPossible = false;
// console.log('this game is not possible');
}
if( outcome[1] > highestRedOutcome ) {
highestRedOutcome = outcome[1];
}
} else if( outcome[0] === 'green' ) {
if( outcome[1] > greenLimit ) {
gameIsPossible = false;
// console.log('this game is not possible');
}
if( outcome[1] > highestGreenOutcome ) {
highestGreenOutcome = outcome[1];
}
} else if( outcome[0] === 'blue' ) {
if( outcome[1] > blueLimit ) {
gameIsPossible = false;
// console.log('this game is not possible');
}
if( outcome[1] > highestBlueOutcome ) {
highestBlueOutcome = outcome[1];
}
}
});
// console.log('highestRedOutcome',highestRedOutcome);
// console.log('highestGreenOutcome',highestGreenOutcome);
// console.log('highestBlueOutcome',highestBlueOutcome);
const powerOfHighestOutcomes = highestRedOutcome * highestGreenOutcome * highestBlueOutcome;
// console.log('powerOfHighestOutcomes',powerOfHighestOutcomes);
outcomePowers.push(powerOfHighestOutcomes);
if( gameIsPossible ) {
possibleGameIDs.push(gameID);
};
});
console.log('possibleGameIDs', possibleGameIDs);
var total = 0;
possibleGameIDs.forEach(function(gameID){
total = total + +gameID;
});
console.log('total',total);
var powersTotal = 0;
console.log('outcomePowers',outcomePowers);
outcomePowers.forEach(function(outcomePwr){
powersTotal = powersTotal + +outcomePwr;
});
console.log('powersTotal',powersTotal);