1
0
Fork 0
This commit is contained in:
Marcus Penate 2022-12-03 20:07:00 -05:00
parent cef11b36ff
commit 63db42f55a
3 changed files with 75 additions and 1 deletions

36
Day3/part1.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
int total = 0;
string line;
while(getline(cin, line))
{
string half1 = line.substr(0, line.size()/2);
string half2 = line.substr(line.size()/2);
sort(half1.begin(), half1.end());
sort(half2.begin(), half2.end());
string remaining;
set_intersection(half1.begin(), half1.end(), half2.begin(), half2.end(), back_inserter(remaining));
int value;
if (remaining[0] >= 'a')
{
value = remaining[0]-'a'+1;
}
else
{
value = remaining[0]-'A'+27;
}
total += value;
}
cout << total << endl;
}

38
Day3/part2.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
int total = 0;
string line1,line2,line3;
while(getline(cin, line1))
{
getline(cin, line2);
getline(cin, line3);
sort(line1.begin(), line1.end());
sort(line2.begin(), line2.end());
sort(line3.begin(), line3.end());
string first_intersection, second_intersection;
set_intersection(line1.begin(), line1.end(), line2.begin(), line2.end(), back_inserter(first_intersection));
set_intersection(first_intersection.begin(), first_intersection.end(), line3.begin(), line3.end(), back_inserter(second_intersection));
int value;
if (second_intersection[0] >= 'a')
{
value = second_intersection[0]-'a'+1;
}
else
{
value = second_intersection[0]-'A'+27;
}
total += value;
}
cout << total << endl;
}

View File

@ -4,4 +4,4 @@ Advent of Code for 2022
# Branch format
Uploading code files at partX.cpp in each days folder. Compile the part with `g++ partX.cpp -o partX`. Download your input file as `inptuX` and run the program with `./partX < inputX`.
Uploading code files at partX.cpp in each day's folder. Compile the part with `g++ partX.cpp -o partX`. Download your input file as `inptuX` and run the program with `./partX < inputX`.