2022 Day 6
This commit is contained in:
parent
67ab12d6ba
commit
9d11afb01d
1 changed files with 28 additions and 0 deletions
28
2022/day6.cpp
Normal file
28
2022/day6.cpp
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
/* Compile using `clang day6.cpp -o day6 -lstdc++ -std=c++11` */
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
|
int find_marker(std::string input, int size) {
|
||||||
|
auto start = input.begin();
|
||||||
|
while (start + size != input.end()) {
|
||||||
|
auto group = std::set<char>(start, start + size);
|
||||||
|
if (group.size() == size) {
|
||||||
|
return start - input.begin() + size;
|
||||||
|
}
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::string input;
|
||||||
|
std::ifstream file("day6.input");
|
||||||
|
std::getline(file, input);
|
||||||
|
|
||||||
|
std::cout << "Part 1: First marker after " << find_marker(input, 4) << " characters\n";
|
||||||
|
std::cout << "Part 2: First marker after " << find_marker(input, 14) << " characters\n";
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue