-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.cpp
64 lines (53 loc) · 1.27 KB
/
Day2.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
#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 );
bool is_valid_game = true;
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" && count > 12) ||
(color == "green" && count > 13 ) ||
(color == "blue" && count > 14 )
)
{
is_valid_game = false;
break;
}
}
if( is_valid_game == false ){ break; }
}
if( is_valid_game )
{
answer += gameid;
}
}
std::cout << answer << "\n";
return 0;
}