-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.cpp
More file actions
48 lines (39 loc) · 1.17 KB
/
Copy pathday6.cpp
File metadata and controls
48 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Example input: mjqjpqmgbljsphdztnvjfqwrcgsmlb - Output: 5 (Ex1)
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
const std::string inputFilePath = "inputs/day6.txt";
bool doesStringHaveUniqueChars(std::string str){
for(int i=0; i < str.length();i++){
for(int j=i+1; j < str.length();j++){
if (str[i] == str[j]){
return false;
}
}
}
return true;
}
int charsToUniqueMessage(std::string input, int charsToCheck){
int charCount = charsToCheck;
for (int i = charsToCheck; i < input.length(); i++)
{
if (doesStringHaveUniqueChars(input.substr(i - charsToCheck, charsToCheck)))
{
break;
}
charCount++;
}
return charCount;
}
int main(){
std::ifstream ifs(inputFilePath, std::ifstream::in);
std::stringstream buffer;
buffer << ifs.rdbuf();
std::string inputStr = buffer.str();
int ex1Chars = charsToUniqueMessage(inputStr, 4);
int ex2Chars = charsToUniqueMessage(inputStr, 14);
std::cout << "--Ex1 Output: " << ex1Chars << std::endl;
std::cout << "--Ex2 Output: " << ex2Chars << std::endl;
return 0;
}