parent
8fa4e90613
commit
d58841276b
3 changed files with 43 additions and 4 deletions
@ -1,9 +1,45 @@ |
|||||||
#include "Day03.h" |
#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() { |
Result Day03::Task1() { |
||||||
return Day::Task1(); |
int sum = 0; |
||||||
|
for (string line : input){ |
||||||
|
int n = line.size(); |
||||||
|
set<char> c1(line.begin(), line.begin() + n / 2); |
||||||
|
set<char> 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() { |
Result Day03::Task2() { |
||||||
return Day::Task2(); |
int sum = 0; |
||||||
} |
|
||||||
|
for (int i = 0; i < input.size(); i += 3){ |
||||||
|
set<char> r1(input[i].begin(), input[i].end()); |
||||||
|
set<char> r2(input[i + 1].begin(), input[i + 1].end()); |
||||||
|
set<char> r3(input[i + 2].begin(), input[i + 2].end()); |
||||||
|
|
||||||
|
set<char> 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); |
||||||
|
} |
||||||
|
Loading…
Reference in new issue