aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
Diffstat (limited to '2022')
-rw-r--r--2022/src/day02.cpp85
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>
6using namespace std;
7
8vector<tuple<int, int>>
9read_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
21void
22part1(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
46void
47part2(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
79int
80main()
81{
82 auto lines = read_file("data/day02.txt");
83 part1(lines);
84 part2(lines);
85}