parent
20eec50236
commit
bd8ceeda1a
3 changed files with 40 additions and 4 deletions
@ -1,9 +1,41 @@ |
||||
#include "Day06.h" |
||||
|
||||
Result Day06::Task1() { |
||||
return Day::Task1(); |
||||
return to_string(findMarker(4)); |
||||
} |
||||
|
||||
Result Day06::Task2() { |
||||
return Day::Task2(); |
||||
return to_string(findMarker(14)); |
||||
} |
||||
|
||||
int Day06::findDuplicate(string value, char* dOut, int mSize) { |
||||
map<char, int> charPos; |
||||
for (int i = 0; i < mSize; i++){ |
||||
char c = value[i]; |
||||
if (charPos.contains(c)){ |
||||
*dOut = c; |
||||
return charPos[c]; |
||||
} |
||||
charPos.insert({c, i}); |
||||
} |
||||
return -1; |
||||
} |
||||
|
||||
int Day06::findMarker(int markerSize) const{ |
||||
string signal = input[0]; |
||||
|
||||
int pos = 0; |
||||
do{ |
||||
string marker = signal.substr(pos, markerSize); |
||||
|
||||
char d; |
||||
int dPos = findDuplicate(marker, &d, markerSize); |
||||
if (dPos == -1) |
||||
return pos + markerSize; |
||||
|
||||
pos += dPos + 1; |
||||
} |
||||
while (pos < signal.size() - markerSize + 1); |
||||
|
||||
return -1; |
||||
} |
Loading…
Reference in new issue