diff --git a/src/days/03/Day03.cpp b/src/days/03/Day03.cpp index c392912..9f0f672 100644 --- a/src/days/03/Day03.cpp +++ b/src/days/03/Day03.cpp @@ -1,9 +1,45 @@ #include "Day03.h" +int Day03::getPriority(char value){ + int a = 'a'; + int A = 'A'; + + if (a < value && value < a + 26) + return value - a + 1; + return value - A + 1 + 26; +} + Result Day03::Task1() { - return Day::Task1(); + int sum = 0; + for (string line : input){ + int n = line.size(); + set c1(line.begin(), line.begin() + n / 2); + set c2(line.begin() + n / 2, line.end()); + + char result; + std::set_intersection(c1.begin(), c1.end(), c2.begin(), c2.end(), &result); + + sum += getPriority(result); + } + return to_string(sum); } Result Day03::Task2() { - return Day::Task2(); -} \ No newline at end of file + int sum = 0; + + for (int i = 0; i < input.size(); i += 3){ + set r1(input[i].begin(), input[i].end()); + set r2(input[i + 1].begin(), input[i + 1].end()); + set r3(input[i + 2].begin(), input[i + 2].end()); + + set is1; + std::set_intersection(r1.begin(), r1.end(), r2.begin(), r2.end(), inserter(is1, is1.begin())); + + char result; + std::set_intersection(is1.begin(), is1.end(), r3.begin(), r3.end(), &result); + + sum += getPriority(result); + } + + return to_string(sum); +} diff --git a/src/days/03/Day03.h b/src/days/03/Day03.h index 4be91a5..147841a 100644 --- a/src/days/03/Day03.h +++ b/src/days/03/Day03.h @@ -7,4 +7,6 @@ protected: Result Task1() override; Result Task2() override; + + static int getPriority(char); }; \ No newline at end of file diff --git a/src/util.h b/src/util.h index 58cd494..ff8d543 100644 --- a/src/util.h +++ b/src/util.h @@ -5,7 +5,8 @@ #include #include #include +#include using std::stoi, std::to_string; using std::cout, std::endl; -using std::string, std::vector; \ No newline at end of file +using std::string, std::vector, std::set; \ No newline at end of file