-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
88 lines (73 loc) · 2.11 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "../Utils/Utils.h"
auto Part1(const std::vector<std::string>& input)
{
Utils::Timer timer("Part 1");
std::regex regexExpression(R"(Game\s[\d]+|[\d]+\s\w+)");
std::map<std::string, int> coloredCubeAmounts = {
{"red", 12},
{"green", 13},
{"blue", 14}
};
int validGamesIDTotalSum = 0;
for (std::string line : input)
{
int currentGameNumber = 0;
bool isValid = true;
// Loop through all matches in current line
for (std::sregex_iterator i = std::sregex_iterator(line.begin(), line.end(), regexExpression); i != std::sregex_iterator(); i++)
{
std::smatch m = *i;
if (m.position() == 0)
{
// Parse game number
currentGameNumber = stoi(std::regex_replace(m.str(), std::regex(R"([\D])"), "")); // Remove all non-digits
}
else
{
// Parse the color and amount
int amount = stoi(std::regex_replace(m.str(), std::regex(R"([\D])"), "")); // Matches all non-digits to replace with nothing. (Keeps digits)
std::string color = std::regex_replace(m.str(), std::regex(R"([\W\d])"), ""); // Matches all non-words and digits to replace with nothing. (Keeps only words, no digits)
// Check if color amount is invalid
if (coloredCubeAmounts[color] < amount)
{
isValid = false;
}
}
}
if (isValid)
{
// Remember want the sum of all valid game numbers
validGamesIDTotalSum += currentGameNumber;
}
}
return validGamesIDTotalSum;
}
auto Part2(const std::vector<std::string>& input)
{
Utils::Timer timer("Part 2");
return 0;
}
int main(int argc, char** argv)
{
Utils::Timer timer("Day 02");
try
{
#ifdef INPUT_TESTING
std::vector<std::string> part1Input = Utils::ReadFile("PuzzleTestInput1.txt");
std::vector<std::string> part2Input = Utils::ReadFile("PuzzleTestInput2.txt");
#else
std::vector<std::string> part1Input = Utils::ReadFile("PuzzleInput.txt");
std::vector<std::string> part2Input = part1Input;
#endif
auto result1 = Part1(part1Input);
Utils::CheckResult(result1, 8);
auto result2 = Part2(part2Input);
Utils::CheckResult(result2, 0);
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}
return 0;
}