Skip to content

Commit

Permalink
Solution for 2015 Day 5 part 2 (C++)
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Lukas <[email protected]>
  • Loading branch information
SebaLukas committed Jan 26, 2023
1 parent c94afb6 commit 543172d
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions 2015/Day5/day5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,62 @@ void part1() {
std::cout << "Nice Strings: " << nice_strings << "\n";
}

bool check_xyy(std::string line) {
for (std::string::size_type i = 0; i < line.size()-2; i++) {
if (line[i] == line[i+2]) {
return true;
}
}
return false;
}

bool find_pair_of_2_letters(std::string line) {

std::string letters;
int found_letters = 0;
std::string::size_type start_pos = 0;

for (std::string::size_type i = 0; i < line.size()-2; i++) {
letters = line.substr(i, 2);

while (std::string::npos != (start_pos = line.find(letters, start_pos))) {
start_pos+=2;
found_letters++;
if (found_letters >= 2) return true;
}
start_pos = 0;
found_letters = 0;
}
return false;
}

void part2() {

std::ifstream input {"input.txt"};
std::string line;
int nice_strings = 0;

if (input.good()) {
while (input >> line) {

if (check_xyy(line) == false) {
continue;
}

if (find_pair_of_2_letters(line) == false) {
continue;
}
nice_strings++;
}
}
std::cout << "Nice Strings: " << nice_strings << "\n";

}

int main() {

part1();

part2();

}

0 comments on commit 543172d

Please sign in to comment.