Advent of Code 2025, done in C++
at main 2.0 kB view raw
1#include "common/getinputpath.h" 2#include <algorithm> 3#include <fstream> 4#include <print> 5#include <string> 6#include <vector> 7 8#ifndef DATA_FOLDER 9#error "DATA_FOLDER is not defined. Check Meson configuration." 10#endif // !DATA_FOLDER 11 12char biggest_char_available(std::string input, int reserved_begin, 13 int reserved_end) { 14 std::string substring = input.substr( 15 reserved_begin, input.length() - reserved_end - reserved_begin); 16 std::vector<char> chars(substring.begin(), substring.end()); 17 std::vector<char> chars_sorted(chars); 18 std::sort(chars_sorted.begin(), chars_sorted.end()); 19 std::reverse(chars_sorted.begin(), chars_sorted.end()); 20 21 auto idx_of_biggest = substring.find_first_of(chars_sorted[0]); 22 return chars[idx_of_biggest]; 23} 24 25// runtime on Ryzen 5 5600G: 0.005s 26int main() { 27 std::ifstream file(get_input_path(DATA_FOLDER)); 28 if (!file.is_open()) { 29 std::println("Could not open the file!"); 30 return 1; 31 } 32 33 long long password_part_1 = 0; 34 long long password_part_2 = 0; 35 36 for (std::string t; std::getline(file, t);) { 37 // part 1 38 { 39 auto tens = biggest_char_available(t, 0, 1); 40 auto tens_idx = t.find_first_of(tens); 41 42 auto ones = biggest_char_available(t, tens_idx + 1, 0); 43 44 std::string result_str = std::string() + tens + ones; 45 long long result = std::stoll(result_str); 46 std::println("{}", result_str); 47 48 password_part_1 += result; 49 } 50 51 // part 2 52 { 53 int reserved_begin = 0; 54 std::string result{}; 55 for (int reserved_end = 11; reserved_end >= 0; --reserved_end) { 56 auto ch = biggest_char_available(t, reserved_begin, reserved_end); 57 reserved_begin += t.substr(reserved_begin).find_first_of(ch) + 1; 58 result += ch; 59 } 60 std::println("p2: {}", result); 61 password_part_2 += std::stoll(result); 62 } 63 } 64 std::println("Eureka! {} / {}", password_part_1, password_part_2); 65 return 0; 66}