-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2-2.cpp
63 lines (52 loc) · 1.31 KB
/
Day2-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
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
int main()
{
std::ifstream input_file( "../inputs/Day2.txt" );
int answer = 0;
while( input_file )
{
std::string dummy_token;
// "Game"
input_file >> dummy_token;
if( dummy_token != "Game" ){ break; }
int gameid;
input_file >> gameid;
// ":"
input_file >> dummy_token;
std::string line;
std::getline( input_file, line );
int max_red = 0;
int max_green = 0;
int max_blue = 0;
std::string game;
std::istringstream line_stream( std::move(line) );
while( std::getline(line_stream, game, ';') )
{
std::istringstream game_stream( std::move(game) );
std::string cube;
while( std::getline( game_stream, cube, ',' ) )
{
int count;
std::string color;
std::istringstream cube_stream( std::move(cube) );
cube_stream >> count >> color;
if( color == "red" )
{
max_red = std::max( max_red, count );
}else if( color == "green" )
{
max_green = std::max( max_green, count );
}else if( color == "blue" )
{
max_blue = std::max( max_blue, count );
}
}
}
answer += max_red * max_green * max_blue;
}
std::cout << answer << "\n";
return 0;
}