-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.cpp
44 lines (33 loc) · 1.03 KB
/
day2.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
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int calculate(char play, char other);
int main() {
cout << "Day 2" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2022\inputs\day2.txt)");
string str;
int totalScore1 = 0;
int totalScore2 = 0;
while (getline(file, str)) {
char other = str.at(0);
char play1 = str.at(2);
char play2 = (other + ('X' - 'A')) + (str.at(2) - 'Y');
play2 = (play2 == ('X' - 1)) ? 'Z' : (play2 == ('Z' + 1)) ? 'X' : play2;
totalScore1 += calculate(play1, other);
totalScore2 += calculate(play2, other);
}
cout << "Part 1: " << totalScore1 << endl;
cout << "Part 2: " << totalScore2 << endl;
return 0;
}
int calculate(char play, char other) {
int score = 0;
int difference = ((play - 'X') - (other - 'A'));
difference = (difference == 2) ? -1 : (difference == -2) ? 1 : difference;
difference++;
score += 3 * difference;
score += (play - 'X') + 1;
return score;
}