diff --git a/Day3/part1.cpp b/Day3/part1.cpp new file mode 100644 index 0000000..889f3a2 --- /dev/null +++ b/Day3/part1.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/Day3/part2.cpp b/Day3/part2.cpp new file mode 100644 index 0000000..293825f --- /dev/null +++ b/Day3/part2.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/README.md b/README.md index c32d2fb..a3da1c6 100644 --- a/README.md +++ b/README.md @@ -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`. \ No newline at end of file +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`. \ No newline at end of file