diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-03 23:35:54 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-03 23:35:54 +0100 |
| commit | 56e890cec0a28c0a485212ccebfaf774235a79a2 (patch) | |
| tree | d9c6241a1aa247e06ab5ba2f6f11967b77d458ec /src/day02.cpp | |
| parent | e9a1cb0441d137d7a26cb303b7e72d3424b7392d (diff) | |
| download | advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.gz advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.tar.bz2 advent-of-code-56e890cec0a28c0a485212ccebfaf774235a79a2.zip | |
prepare for more puzzles ... :)
Diffstat (limited to 'src/day02.cpp')
| -rw-r--r-- | src/day02.cpp | 100 |
1 files changed, 0 insertions, 100 deletions
diff --git a/src/day02.cpp b/src/day02.cpp deleted file mode 100644 index df560c3..0000000 --- a/src/day02.cpp +++ /dev/null | |||
| @@ -1,100 +0,0 @@ | |||
| 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 | } | ||
