-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.cpp
85 lines (58 loc) · 1.75 KB
/
day3.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Helpers/HelperFunctions.h"
void calculatePriorities(int &ans, vector<char> &chars);
int main() {
cout << "Day 3" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2022\inputs\day3.txt)");
string str;
int ans1 = 0;
vector<string> data;
while (getline(file, str)) {
data.push_back(str);
}
vector<char> duplicates;
auto it = data.begin();
while (it != data.end()) {
string s = *it;
string half = s.substr(0, s.length()/2);
string otherHalf = s.substr(s.length()/2);
vector<char> charHalf(half.begin(), half.end());
vector<char> charOtherHalf(otherHalf.begin(), otherHalf.end());
vector<char> result = get_intersection(charHalf, charOtherHalf);
duplicates.push_back(result[0]);
it++;
}
calculatePriorities(ans1, duplicates);
cout << "Part 1: " << ans1 << endl;
int ans2 = 0;
it = data.begin();
duplicates.clear();
while (it != data.end()) {
string lineA = *it;
string lineB = *(it+1);
string lineC = *(it+2);
vector<char> charA(lineA.begin(), lineA.end());
vector<char> charB(lineB.begin(), lineB.end());
vector<char> charC(lineC.begin(), lineC.end());
vector<char> result = get_intersection(charA, charB);
vector<char> finalResult = get_intersection(result, charC);
duplicates.push_back(finalResult[0]);
it += 3;
}
calculatePriorities(ans2, duplicates);
cout << "Part 2: " << ans2 << endl;
return 0;
}
void calculatePriorities(int &ans, vector<char> &chars) {
for (auto& it : chars) {
if (it < 'a') { // 'A'
ans += (it - 'A') + 27;
} else { // 'a'
ans += (it - 'a') + 1;
}
}
}