From 543172da0e924c7efa34ef930d0592a7cde2701b Mon Sep 17 00:00:00 2001 From: Sebastian Lukas Date: Thu, 26 Jan 2023 09:23:39 +0100 Subject: [PATCH] Solution for 2015 Day 5 part 2 (C++) Signed-off-by: Sebastian Lukas --- 2015/Day5/day5.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/2015/Day5/day5.cpp b/2015/Day5/day5.cpp index 70e923e..95f1d8f 100644 --- a/2015/Day5/day5.cpp +++ b/2015/Day5/day5.cpp @@ -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(); + } \ No newline at end of file