diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/day02.cpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/day02.cpp b/src/day02.cpp new file mode 100644 index 0000000..df560c3 --- /dev/null +++ b/src/day02.cpp | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | #include <cctype> | ||
| 2 | #include <cstdio> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <map> | ||
| 6 | #include <sstream> | ||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | vector<string> | ||
| 12 | split(const string& line, char sep) | ||
| 13 | { | ||
| 14 | vector<string> parts{}; | ||
| 15 | stringstream input{ line }; | ||
| 16 | |||
| 17 | for ( string part; getline(input, part, sep); ) { | ||
| 18 | parts.emplace_back(part); | ||
| 19 | } | ||
| 20 | |||
| 21 | return parts; | ||
| 22 | } | ||
| 23 | |||
| 24 | void | ||
| 25 | part1() | ||
| 26 | { | ||
| 27 | fstream input{ "data/day02.txt" }; | ||
| 28 | |||
| 29 | auto sum = 0; | ||
| 30 | for ( string line; getline(input, line); ) { | ||
| 31 | auto game = split(line, ':'); | ||
| 32 | |||
| 33 | auto gameId = 0; | ||
| 34 | sscanf(game[0].data(), "Game %d", &gameId); // NOLINT | ||
| 35 | |||
| 36 | auto subsets = split(game[1], ';'); | ||
| 37 | |||
| 38 | auto fail = false; | ||
| 39 | |||
| 40 | for ( const auto& subset: subsets ) { | ||
| 41 | auto cubes = split(subset, ','); | ||
| 42 | |||
| 43 | map<string, int> counts{}; | ||
| 44 | |||
| 45 | for ( const auto& cube: cubes ) { | ||
| 46 | auto data = split(cube, ' '); | ||
| 47 | |||
| 48 | auto count = stoi(data[1]); | ||
| 49 | auto color = data[2]; | ||
| 50 | |||
| 51 | counts[color] += count; | ||
| 52 | } | ||
| 53 | |||
| 54 | fail |= (counts["red"] > 12) || (counts["green"] > 13) || (counts["blue"] > 14); // NOLINT | ||
| 55 | } | ||
| 56 | |||
| 57 | if ( !fail ) { | ||
| 58 | sum += gameId; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | cout << sum << endl; | ||
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | part2() | ||
| 66 | { | ||
| 67 | fstream input{ "data/day02.txt" }; | ||
| 68 | |||
| 69 | auto sum = 0; | ||
| 70 | |||
| 71 | for ( string line; getline(input, line); ) { | ||
| 72 | auto game = split(line, ':'); | ||
| 73 | |||
| 74 | auto subsets = split(game[1], ';'); | ||
| 75 | |||
| 76 | map<string, int> colors{}; | ||
| 77 | |||
| 78 | for ( const auto& subset: subsets ) { | ||
| 79 | auto cubes = split(subset, ','); | ||
| 80 | |||
| 81 | for ( const auto& cube: cubes ) { | ||
| 82 | auto data = split(cube, ' '); | ||
| 83 | |||
| 84 | auto count = stoi(data[1]); | ||
| 85 | auto color = data[2]; | ||
| 86 | |||
| 87 | colors[color] = max(colors[color], count); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | sum += colors["red"] * colors["green"] * colors["blue"]; | ||
| 91 | } | ||
| 92 | cout << sum << endl; | ||
| 93 | } | ||
| 94 | |||
| 95 | int | ||
| 96 | main() | ||
| 97 | { | ||
| 98 | part1(); | ||
| 99 | part2(); | ||
| 100 | } | ||
