Added day 6 (two programs. had to rewrite for the second problem). Added starter files.
This commit is contained in:
56
2022/6/main.cpp
Normal file
56
2022/6/main.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
bool Unique(const std::deque<char> &buf)
|
||||
{
|
||||
std::set<char> s;
|
||||
for(const char &ch : buf)
|
||||
s.insert(ch);
|
||||
return (s.size() == 4);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream ifs("data.txt");
|
||||
if(!ifs.is_open())
|
||||
{
|
||||
std::cerr << "Missing data.txt." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for(std::string line; std::getline(ifs, line); )
|
||||
{
|
||||
if(line == "")
|
||||
continue;
|
||||
|
||||
std::deque<char> buf;
|
||||
|
||||
int idx = 0;
|
||||
for(const char &ch : line)
|
||||
{
|
||||
++idx;
|
||||
buf.push_back(ch);
|
||||
if(buf.size() >= 4)
|
||||
{
|
||||
while(buf.size() > 4)
|
||||
buf.pop_front();
|
||||
|
||||
if(Unique(buf))
|
||||
{
|
||||
std::cout << "At index " << idx << ": ";
|
||||
for(const char &ch : buf)
|
||||
std::cout << ch;
|
||||
std::cout << std::endl;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user