-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02_part02.ixx
99 lines (83 loc) · 2.37 KB
/
day02_part02.ixx
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
#include <vector>
#include <string>
#include <regex>
#include <iostream>
#include <sstream>
#include <unordered_map>
import Utilities;
using namespace std;
export module day02_part02;
namespace day02_part02 {
class GameSet {
public:
GameSet(int id, vector<std::unordered_map<std::string, int>> cubes)
: _idGame(id), _gameCubes(cubes)
{
}
int idGame() const {
return _idGame;
}
vector<std::unordered_map<std::string, int>> gameCubes() const {
return _gameCubes;
}
private:
int _idGame;
vector<std::unordered_map<std::string, int>> _gameCubes;
};
static GameSet buildGameSet(string line) {
int gameId = 0;
vector<std::unordered_map<std::string, int>> gameCubes;
// extract game part
std::istringstream gameParts(line);
std::vector<std::string> parts;
std::string part;
while (std::getline(gameParts, part, ' ')) {
parts.push_back(part);
}
gameId = stoi(parts[1]);
// extract cubes part
regex rgx("(( \\d+ (blue|green|red),\\s*)+)?( \\d+ (blue|green|red)\\s*)");
std::sregex_iterator iter(line.begin(), line.end(), rgx);
std::sregex_iterator end;
while (iter != end) {
std::istringstream cubeSet(iter->str());
std::string cube;
std::unordered_map<std::string, int> cubesOfCubSet;
while (std::getline(cubeSet, cube, ',')) {
std::istringstream partDef(cube);
string numOfCubes;
string color;
partDef >> numOfCubes >> color;
cubesOfCubSet[color] = stoi(numOfCubes);
}
// Move to the next match
++iter;
gameCubes.push_back(cubesOfCubSet);
}
return GameSet(gameId, gameCubes);
}
export int execute() {
vector<string> lines = Utilities::readTextFile("day02/day02_input.txt");
//vector<string> lines = Utilities::readTextFile("day02/test_input_01.txt");
int result = 0;
vector<GameSet> gameSets;
for (string line : lines)
gameSets.push_back(buildGameSet(line));
int sumOfCubes = 0;
std::unordered_map<std::string, int> miniumCubes;
for (GameSet gameSet : gameSets) {
miniumCubes["red"] = 0;
miniumCubes["green"] = 0;
miniumCubes["blue"] = 0;
for (std::unordered_map<std::string, int> cubes : gameSet.gameCubes()) {
for (auto const& [key, val] : cubes) {
int currentVal = miniumCubes[key];
if (currentVal < val)
miniumCubes[key] = val;
}
}
sumOfCubes += miniumCubes["red"] * miniumCubes["green"] * miniumCubes["blue"];
}
return sumOfCubes;
}
}