#include "common/getinputpath.h" #include #include #include #include #include #ifndef DATA_FOLDER #error "DATA_FOLDER is not defined. Check Meson configuration." #endif // !DATA_FOLDER char biggest_char_available(std::string input, int reserved_begin, int reserved_end) { std::string substring = input.substr( reserved_begin, input.length() - reserved_end - reserved_begin); std::vector chars(substring.begin(), substring.end()); std::vector chars_sorted(chars); std::sort(chars_sorted.begin(), chars_sorted.end()); std::reverse(chars_sorted.begin(), chars_sorted.end()); auto idx_of_biggest = substring.find_first_of(chars_sorted[0]); return chars[idx_of_biggest]; } // runtime on Ryzen 5 5600G: 0.005s int main() { std::ifstream file(get_input_path(DATA_FOLDER)); if (!file.is_open()) { std::println("Could not open the file!"); return 1; } long long password_part_1 = 0; long long password_part_2 = 0; for (std::string t; std::getline(file, t);) { // part 1 { auto tens = biggest_char_available(t, 0, 1); auto tens_idx = t.find_first_of(tens); auto ones = biggest_char_available(t, tens_idx + 1, 0); std::string result_str = std::string() + tens + ones; long long result = std::stoll(result_str); std::println("{}", result_str); password_part_1 += result; } // part 2 { int reserved_begin = 0; std::string result{}; for (int reserved_end = 11; reserved_end >= 0; --reserved_end) { auto ch = biggest_char_available(t, reserved_begin, reserved_end); reserved_begin += t.substr(reserved_begin).find_first_of(ch) + 1; result += ch; } std::println("p2: {}", result); password_part_2 += std::stoll(result); } } std::println("Eureka! {} / {}", password_part_1, password_part_2); return 0; }