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
|
#include "game.h"
#include <iostream>
#include <sstream>
Game::Game(int id) : id_{ id }
{
}
void Game::push_back(const std::vector<int>& draw)
{
draws.push_back(draw);
}
bool Game::is_valid(int blue, int red, int green) const
{
for (const auto& draw : draws)
{
if (draw[0] > blue or draw[1] > red or draw[2] > green)
{
return false;
}
}
return true;
}
int Game::power() const
{
int blue{}, red{}, green{};
for (const auto& draw : draws)
{
blue = std::max(draw[0], blue);
red = std::max(draw[1], red);
green = std::max(draw[2], green);
}
return blue * red * green;
}
std::string Game::to_string() const
{
std::ostringstream out;
out << "Game " << id_ << ": ";
for (const auto& draw : draws)
{
out << draw[0] << " " << draw[1] << " " << draw[2] << ";";
}
return out.str();
}
Game Game::from_string(const std::string& line)
{
std::istringstream in(line);
std::string id;
in >> skip<game_t> >> id;
id.pop_back();
Game game{ std::stoi(id) };
int n;
std::string kind;
std::vector<int> draw{ 0, 0, 0};
while (in >> n >> kind)
{
char sep{ kind.back() };
if (sep == ';' or sep == ',')
{
kind.pop_back();
}
else
{
sep = ';';
}
if (kind == "blue")
{
draw[0] += n;
}
else if (kind == "red")
{
draw[1] += n;
}
else if (kind == "green")
{
draw[2] += n;
}
if (sep == ';')
{
game.push_back(draw);
draw = { 0, 0, 0 };
}
}
return game;
}
|