-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.cpp
120 lines (105 loc) · 2.66 KB
/
2.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
#include <iostream>
#include <fstream>
#include <algorithm>
struct Set {
int red = 0;
int green = 0;
int blue = 0;
void clear() {
red = 0;
green = 0;
blue = 0;
}
int product() {
return red * green * blue;
}
void update_max(const Set& s) {
red = std::max(red, s.red);
green = std::max(green, s.green);
blue = std::max(blue, s.blue);
}
};
bool operator<=(const Set& s1, const Set& s2) {
return (s1.red <= s2.red) && (s1.green <= s2.green) && (s1.blue <= s2.blue);
}
std::ostream& operator<<(std::ostream& stream, const Set& s) {
std::cout << "(" << s.red << "," << s.green << "," << s.blue << ")";
return stream;
}
void process_group(Set& s1, const std::string& line, int l, int r) {
int n = std::stoi(line.substr(l, r - l));
while (line[l] != ' ') ++l;
++l;
std::string colour = line.substr(l, r - l);
if (colour == "red") s1.red = n;
else if (colour == "green") s1.green = n;
else if (colour == "blue") s1.blue = n;
else throw;
}
int part1(const std::string& line) {
Set s1;
const static Set s2{12, 13, 14};
int i = 0;
while (line[i] != ' ') ++i;
int game = 0;
int j = i + 1;
while (line[j] != ':') {
game = game * 10 + (line[j] - '0');
++j;
}
i = j;
std::cout << game << " ";
while (i < line.size()) {
s1.clear();
j = i + 1;
while (j < line.size() && line[j] != ';') ++j;
for (int k = i + 1; k <= j; ++k) {
if (k == j || line[k] == ',' || line[k] == ';') {
process_group(s1, line, i + 2, k);
i = k;
}
}
std::cout << s1 << " ";
if (!(s1 <= s2)) {
std::cout << std::endl;
return 0;
}
}
std::cout << std::endl;
return game;
}
int part2(const std::string& line) {
Set s1;
Set s2;
int i = 0;
while (line[i] != ' ') ++i;
int game = 0;
int j = i + 1;
while (line[j] != ':') {
game = game * 10 + (line[j] - '0');
++j;
}
i = j;
while (i < line.size()) {
s1.clear();
j = i + 1;
while (j < line.size() && line[j] != ';') ++j;
for (int k = i + 1; k <= j; ++k) {
if (k == j || line[k] == ',' || line[k] == ';') {
process_group(s1, line, i + 2, k);
i = k;
}
}
s2.update_max(s1);
}
return s2.product();
}
int main() {
std::ifstream f("2.txt");
std::string line;
std::uint64_t ans = 0;
while (std::getline(f, line)) {
ans += part2(line);
}
std::cout << ans << std::endl;
}