-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_two_part_two.cpp
139 lines (103 loc) · 3.36 KB
/
day_two_part_two.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
133
134
135
136
137
138
139
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> split(std::string s, std::string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
token = s.substr (pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back (token);
}
res.push_back (s.substr (pos_start));
return res;
}
enum class Color{
RED,
GREEN,
BLUE,
};
struct revealed_cubes{
int amount;
Color color;
};
struct Set{
std::vector<revealed_cubes> moves;
};
struct highest_case{
int red;
int green;
int blue;
int power(){
return red * green * blue;
}
};
class Game{
public:
int game_id;
std::vector<Set> sets;
Game(int id, std::string sets_string){
std::vector<std::string> sets = split(sets_string, "; ");
std::vector<Set> sets_parsed;
for (auto set: sets){
std::vector<std::string> moves = split(set, ", ");
std::vector<revealed_cubes> moves_parsed;
for (auto move: moves){
std::vector<std::string> parts = split(move, " ");
Color color;
if (parts[1] == "blue"){
color = Color::BLUE;
}else if (parts[1] == "red"){
color = Color::RED;
}else{
color = Color::GREEN;
}
int amount = std::stoi(parts[0]);
revealed_cubes cubes = {amount, color};
moves_parsed.push_back(cubes);
}
Set new_set = {moves_parsed};
sets_parsed.push_back(new_set);
}
this->sets = sets_parsed;
this->game_id = id;
}
highest_case highest_possible(){
int highest_red = 0;
int highest_blue = 0;
int highest_green = 0;
for (auto set: sets){
for (auto move: set.moves){
if(move.color == Color::BLUE && move.amount > highest_blue){
highest_blue = move.amount;
}
if(move.color == Color::RED && move.amount > highest_red){
highest_red = move.amount;
}
if(move.color == Color::GREEN && move.amount > highest_green){
highest_green = move.amount;
}
}
}
return {highest_red, highest_green, highest_blue};
}
};
int main(){
std::ifstream input_file("./input/day_two.txt");
int sum = 0;
for( std::string line; std::getline( input_file, line ); ){
int divider = line.find_first_of(":");
std::vector<std::string> parts = split(line, ":");
std::string sets = parts[1].substr(1);
int space_before_number = parts[0].find_first_of(" ") + 1;
std::string number_in_str = parts[0].substr(space_before_number);
Game current_game(std::stoi(number_in_str), sets);
int highest_power_achievable = current_game.highest_possible().power();
sum += highest_power_achievable;
}
std::cout << "The answer is: [[ " << sum << " ]]" << std::endl;
return 0;
}