Advertisement
msminhas93

aoc_2024_day1

Dec 4th, 2024
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. #include <chrono>
  8.  
  9. std::pair<std::vector<int>, std::vector<int>> create_sorted_vectors_from_file(const std::string& file_name) {
  10.     std::ifstream file(file_name);
  11.     std::vector<int> col1, col2;
  12.     std::string line;
  13.  
  14.     while (std::getline(file, line)) {
  15.         std::istringstream iss(line);
  16.         int num1, num2;
  17.         if (iss >> num1 >> num2) {
  18.             col1.push_back(num1);
  19.             col2.push_back(num2);
  20.         }
  21.     }
  22.  
  23.     std::sort(col1.begin(), col1.end());
  24.     std::sort(col2.begin(), col2.end());
  25.  
  26.     return {col1, col2};
  27. }
  28.  
  29. int64_t calculate_l1_distance(const std::vector<int>& vec1, const std::vector<int>& vec2) {
  30.     int64_t distance = 0;
  31.     for (size_t i = 0; i < vec1.size(); ++i) {
  32.         distance += std::abs(static_cast<int64_t>(vec1[i]) - vec2[i]);
  33.     }
  34.     return distance;
  35. }
  36.  
  37. int64_t find_similarity_score(const std::vector<int>& vec1, const std::vector<int>& vec2) {
  38.     std::unordered_map<int, unsigned int> vec2_counter;
  39.     for (int el : vec2) {
  40.         vec2_counter[el]++;
  41.     }
  42.  
  43.     int64_t score = 0;
  44.     for (int el : vec1) {
  45.         auto it = vec2_counter.find(el);
  46.         if (it != vec2_counter.end()) {
  47.             score += static_cast<int64_t>(el) * it->second;
  48.         }
  49.     }
  50.     return score;
  51. }
  52.  
  53. int main() {
  54.     auto start = std::chrono::high_resolution_clock::now();
  55.  
  56.     try {
  57.         auto [col1, col2] = create_sorted_vectors_from_file(R"(C:\Users\msmin\code\aoc_python\day1_input.txt)");
  58.  
  59.         if (col1.size() == col2.size()) {
  60.             int64_t l1_distance = calculate_l1_distance(col1, col2);
  61.             int64_t similarity_score = find_similarity_score(col1, col2);
  62.             std::cout << "L1 distance between the two vectors: " << l1_distance << std::endl;
  63.             std::cout << "similarity score between the two vectors: " << similarity_score << std::endl;
  64.         } else {
  65.             std::cout << "Error: Vectors have different lengths" << std::endl;
  66.         }
  67.     } catch (const std::exception& e) {
  68.         std::cout << "Error reading file: " << e.what() << std::endl;
  69.     }
  70.  
  71.     auto end = std::chrono::high_resolution_clock::now();
  72.     auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  73.     std::cout << "Time elapsed: " << duration.count() << " microseconds" << std::endl;
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement