diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-04-24 23:22:18 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-04-24 23:22:18 +0200 |
| commit | 1088bfbda97eb89bf55fd7cd171726eb7f6ea71f (patch) | |
| tree | 1669471d36a388a1accd96e494f999167c46a159 /2022/src/day02.cpp | |
| parent | 5b9d1245f073f858e81b5a476463a948111a8a73 (diff) | |
| download | advent-of-code-1088bfbda97eb89bf55fd7cd171726eb7f6ea71f.tar.gz advent-of-code-1088bfbda97eb89bf55fd7cd171726eb7f6ea71f.tar.bz2 advent-of-code-1088bfbda97eb89bf55fd7cd171726eb7f6ea71f.zip | |
day02, advent of code 2022
Diffstat (limited to '2022/src/day02.cpp')
| -rw-r--r-- | 2022/src/day02.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/2022/src/day02.cpp b/2022/src/day02.cpp new file mode 100644 index 0000000..65146d5 --- /dev/null +++ b/2022/src/day02.cpp | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <string> | ||
| 5 | #include <vector> | ||
| 6 | using namespace std; | ||
| 7 | |||
| 8 | vector<tuple<int, int>> | ||
| 9 | read_file(string_view filename) | ||
| 10 | { | ||
| 11 | fstream input{ filename }; | ||
| 12 | vector<tuple<int, int>> data; | ||
| 13 | |||
| 14 | for ( string line; getline(input, line); ) { | ||
| 15 | data.emplace_back(line[0] - 'A' + 1, line[2] - 'X' + 1); | ||
| 16 | } | ||
| 17 | |||
| 18 | return data; | ||
| 19 | } | ||
| 20 | |||
| 21 | void | ||
| 22 | part1(const vector<tuple<int, int>>& lines) | ||
| 23 | { | ||
| 24 | map<int, int> wins{ | ||
| 25 | { 1, 2 }, | ||
| 26 | { 2, 3 }, | ||
| 27 | { 3, 1 } | ||
| 28 | }; | ||
| 29 | |||
| 30 | static const int INC_DRAW = 3; | ||
| 31 | static const int INC_WIN = 6; | ||
| 32 | |||
| 33 | int score = 0; | ||
| 34 | for ( auto [a, b]: lines ) { | ||
| 35 | score += b; | ||
| 36 | if ( a == b ) { | ||
| 37 | score += INC_DRAW; | ||
| 38 | } | ||
| 39 | else if ( wins[a] == b ) { | ||
| 40 | score += INC_WIN; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | cout << score << endl; | ||
| 44 | } | ||
| 45 | |||
| 46 | void | ||
| 47 | part2(const vector<tuple<int, int>>& lines) | ||
| 48 | { | ||
| 49 | map<int, int> wins{ | ||
| 50 | { 1, 2 }, | ||
| 51 | { 2, 3 }, | ||
| 52 | { 3, 1 } | ||
| 53 | }; | ||
| 54 | |||
| 55 | map<int, int> loose{ | ||
| 56 | { 2, 1 }, | ||
| 57 | { 3, 2 }, | ||
| 58 | { 1, 3 } | ||
| 59 | }; | ||
| 60 | |||
| 61 | static const int INC_DRAW = 3; | ||
| 62 | static const int INC_WIN = 6; | ||
| 63 | |||
| 64 | int score = 0; | ||
| 65 | for ( auto [a, b]: lines ) { | ||
| 66 | if (b == 2) { | ||
| 67 | score += a + INC_DRAW; | ||
| 68 | } | ||
| 69 | else if (b == 3) { | ||
| 70 | score += wins[a] + INC_WIN; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | score += loose[a]; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | cout << score << endl; | ||
| 77 | } | ||
| 78 | |||
| 79 | int | ||
| 80 | main() | ||
| 81 | { | ||
| 82 | auto lines = read_file("data/day02.txt"); | ||
| 83 | part1(lines); | ||
| 84 | part2(lines); | ||
| 85 | } | ||
