-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
132 lines (101 loc) · 2.79 KB
/
main.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<string> parseFile(string filename) {
ifstream file(filename);
vector<string> lines;
string line;
while (getline(file, line)) {
lines.push_back(line);
}
return lines;
}
vector<string> splitString(string str, char c) {
vector<string> result;
stringstream ss(str);
string item;
while (getline(ss, item, c)) {
result.push_back(item);
}
return result;
}
bool isSetPossible(string set, map<std::string, int> maxColors) {
vector<string> colorStrings = splitString(set, ',');
for (string colorString: colorStrings) {
vector<string> colorParts = splitString(colorString, ' ');
string colorCount = colorParts[1];
int color = stoi(colorCount);
if (color > maxColors[colorParts[2]]) {
return false;
}
}
return true;
}
bool isGamePossible(string line, map<std::string, int> maxColors) {
vector<string> setParts = splitString(line, ';');
for (string set: setParts) {
if (!isSetPossible(set, maxColors)) {
return false;
}
}
return true;
}
int solve(string filename) {
vector<string> lines = parseFile(filename);
map<std::string, int> maxColors;
maxColors["red"] = 12;
maxColors["green"] = 13;
maxColors["blue"] = 14;
int sum = 0;
for (string line : lines) {
vector<string> parts = splitString(line, ':');
string gameIdString = splitString(parts[0], ' ')[1];
int gameId = stoi(gameIdString);
if (isGamePossible(parts[1], maxColors)) {
sum = sum + gameId;
}
}
return sum;
}
void updateMinColors(string set, map<string, int>& minColors) {
vector<string> colorStrings = splitString(set, ',');
for (string colorString: colorStrings) {
vector<string> colorParts = splitString(colorString, ' ');
string colorCount = colorParts[1];
int color = stoi(colorCount);
if (color > minColors[colorParts[2]]) {
minColors[colorParts[2]] = color;
}
}
}
int powerOfMinimumSetOfCubes(string line) {
vector<string> setParts = splitString(line, ';');
map<string, int> minColors;
minColors["red"] = 0;
minColors["green"] = 0;
minColors["blue"] = 0;
for (string set: setParts) {
updateMinColors(set, minColors);
}
return minColors["red"] * minColors["green"] * minColors["blue"];
}
int solve2(string filename) {
vector<string> lines = parseFile(filename);
map<string, int> maxColors;
int sum = 0;
for (string line : lines) {
vector<string> parts = splitString(line, ':');
string gameIdString = splitString(parts[0], ' ')[1];
int power = powerOfMinimumSetOfCubes(parts[1]);
sum = sum + power;
}
return sum;
}
int main() {
cout << solve("input") << endl;
cout << solve2("input") << endl;
}