Advertisement
Guest User

AoC23-13ab

a guest
Dec 13th, 2023
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. int find_horiz(std::vector<std::string> map, int far) {
  7.   for (int i = 0; i < map.size() - 1; i++) {
  8.     int dist = 0;
  9.     int j = i, k = i + 1;
  10.     while (dist <= far && j >= 0 && k < map.size()) {
  11.       for (int m = 0; m < map[0].size(); m++) {
  12.         dist += (map[j][m] != map[k][m]);
  13.       }
  14.       j--;
  15.       k++;
  16.     }
  17.     if (dist == far) {
  18.       return i + 1;
  19.     }
  20.   }
  21.  
  22.   return 0;
  23. };
  24.  
  25. int find_vert(std::vector<std::string> map, int far) {
  26.   std::vector<std::string> new_map;
  27.   for (int i = 0; i < map[0].size(); i++) {
  28.     std::string temp;
  29.     for (int j = 0; j < map.size(); j++) {
  30.       temp += map[j][i];
  31.     }
  32.     new_map.push_back(temp);
  33.   }
  34.  
  35.   return find_horiz(new_map, far);
  36.  
  37.   return 0;
  38. };
  39.  
  40. int main() {
  41.   std::vector<std::vector<std::string>> maps;
  42.   std::vector<std::string> map;
  43.   std::ifstream input("input.txt");
  44.   if (input.is_open()) {
  45.     while(input.good()) {
  46.       if (input.eof()) {break;}
  47.       std::string temp;
  48.       std::getline(input, temp);
  49.       if (temp != "") {
  50.         map.push_back(temp);
  51.       } else {
  52.         maps.push_back(map);
  53.         map.clear();
  54.       }
  55.     }
  56.   }
  57.  
  58.   int total1 = 0, total2 = 0;
  59.   for (int i = 0; i < maps.size(); i++) {
  60.     if (find_horiz(maps[i], 0)) {total1 += 100 * find_horiz(maps[i], 0);}
  61.     else {total1 += find_vert(maps[i], 0);}
  62.     if (find_horiz(maps[i], 1)) {total2 += 100 * find_horiz(maps[i], 1);}
  63.       else {total2 += find_vert(maps[i], 1);}
  64.   }
  65.   std::cout << "Part 1: " << total1 << std::endl;
  66.   std::cout << "Part 2: " << total2;
  67.  
  68.   return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement