parent
3f65baeccb
commit
ee36ea57db
2 changed files with 62 additions and 3 deletions
@ -1,9 +1,65 @@ |
||||
#include "Day02.h" |
||||
|
||||
Result Day02::Task1() { |
||||
return Day::Task1(); |
||||
int r = 12; |
||||
int g = 13; |
||||
int b = 14; |
||||
|
||||
uint32_t sum = 0; |
||||
uint32_t id = 1; |
||||
for (const Game &game : parseGames()){ |
||||
bool possible = true; |
||||
for (const auto &set : game) |
||||
if (set[0] > r || set[1] > g || set[2] > b) |
||||
possible = false; |
||||
if (possible) |
||||
sum += id; |
||||
|
||||
id++; |
||||
} |
||||
return to_string(sum); |
||||
} |
||||
|
||||
Result Day02::Task2() { |
||||
return Day::Task2(); |
||||
uint32_t sum = 0; |
||||
for (const Game &game : parseGames()){ |
||||
size_t r = 0, g = 0, b = 0; |
||||
for (const auto &set : game){ |
||||
r = std::max(r, set[0]); |
||||
g = std::max(g, set[1]); |
||||
b = std::max(b, set[2]); |
||||
} |
||||
sum += r * g * b; |
||||
} |
||||
return to_string(sum); |
||||
} |
||||
|
||||
vector<Day02::Game> Day02::parseGames() const { |
||||
vector<Game> games; |
||||
for (const string &line : input){ |
||||
auto record = line.substr(line.find(": ") + 1); |
||||
record.push_back(';'); |
||||
auto setIndices = findAll(record, ";"); |
||||
|
||||
Game game; |
||||
size_t start = 0; |
||||
for (size_t i : setIndices){ |
||||
auto set = record.substr(start, i - start); |
||||
|
||||
auto findNumber = [set](const string &type) -> size_t { |
||||
auto pos = set.find(type); |
||||
if (pos != string::npos) |
||||
return std::stoul(set.substr(pos - 3, 2)); |
||||
return 0; |
||||
}; |
||||
size_t r = findNumber("red"); |
||||
size_t g = findNumber("green"); |
||||
size_t b = findNumber("blue"); |
||||
|
||||
game.push_back({r, g, b}); |
||||
start = i; |
||||
} |
||||
games.push_back(game); |
||||
} |
||||
return games; |
||||
} |
Loading…
Reference in new issue