Advertisement
Guest User

AOC 24 Day 6 v1 C++ Solution

a guest
Dec 12th, 2024
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | Source Code | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <unordered_map>
  6.  
  7. const int steps[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  8.  
  9. struct vector_hash {
  10.     std::size_t operator()(std::vector<int> const& vec) const {
  11.         std::size_t seed = vec.size();
  12.         for(auto x : vec) {
  13.             x = ((x >> 16) ^ x) * 0x45d9f3b;
  14.             x = ((x >> 16) ^ x) * 0x45d9f3b;
  15.             x = (x >> 16) ^ x;
  16.             seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  17.         }
  18.         return seed;
  19.     }
  20. };
  21.  
  22. bool search(std::vector<int>& v, int key){
  23.     for(int i = 0; i < v.size(); i++){
  24.         if(v[i] == key)
  25.             return true;
  26.     }
  27.     return false;
  28. }
  29.  
  30. bool stop_check(int x, int y, int nrows, int ncols){
  31.     if(x >= nrows || x < 0 || y < 0 || y >= ncols)
  32.         return true;
  33.     return false;
  34. }
  35.  
  36. void change_pos(int& x, int& y, int cur_dir, char op){
  37.     if(op == '+'){
  38.         x += steps[cur_dir][0];
  39.         y += steps[cur_dir][1];
  40.     } else if(op == '-'){
  41.         x -= steps[cur_dir][0];
  42.         y -= steps[cur_dir][1];
  43.     }
  44. }
  45.  
  46. bool place_obstacle(std::unordered_map<std::vector<int>, std::vector<int>, vector_hash>& visited,     std::vector<std::string>& guard_map, int x, int y, int cur_dir)
  47. {
  48.     std::unordered_map<std::vector<int>, std::vector<int>, vector_hash> lvisited;
  49.     int nrows = guard_map.size();
  50.     int ncols = guard_map[0].size();
  51.     bool quit = false;
  52.     while(!quit){
  53.         while(!(quit = stop_check(x, y, nrows, ncols)) && guard_map[x][y] != '#' ){
  54.             std::vector<int> t{x, y};
  55.             if((visited.find(t) != visited.end() && search(visited[t], cur_dir)) || (lvisited.find(t) != lvisited.end() && search(lvisited[t], cur_dir))){
  56.                 return true;
  57.             }
  58.             lvisited[t].push_back(cur_dir);
  59.             change_pos(x, y, cur_dir, '+');
  60.         }
  61.         change_pos(x, y, cur_dir, '-');
  62.         cur_dir = (cur_dir + 1) % 4;
  63.     }
  64.     return false;
  65. }
  66.  
  67. int main()
  68. {
  69.     std::ifstream inp_file;
  70.     std::vector<std::string> guard_map;
  71.     int st_x, st_y;
  72.     int cur_dir;
  73.     inp_file.open("day6-in.txt");
  74.     if(inp_file.is_open()){
  75.         std::string line;
  76.         bool f = false;
  77.         while(std::getline(inp_file, line)){
  78.             guard_map.push_back(line);
  79.             for(int i = 0; !f && i < line.size(); i++){
  80.                 if(line[i] == '^'){
  81.                     st_x = guard_map.size() - 1;
  82.                     st_y = i;
  83.                     f = true;
  84.                 }
  85.             }
  86.         }
  87.     }
  88.     inp_file.close();
  89.     int nrows, ncols;
  90.     bool quit = false;
  91.     int pos_visited = 1;
  92.     int nobstacle_pos = 0;
  93.     std::unordered_map<std::vector<int>, std::vector<int>, vector_hash> visited;
  94.  
  95.     nrows = guard_map.size();
  96.     ncols = guard_map[0].size();
  97.     int x,y;
  98.     x = st_x;
  99.     y = st_y;
  100.     cur_dir = 0;
  101.     while(!quit){
  102.         while(guard_map[x][y] != '#'){
  103.  
  104.             std::vector<int> t{x, y};
  105.             visited[t].push_back(cur_dir);
  106.  
  107.             if(guard_map[x][y] == '.'){
  108.                 pos_visited++;
  109.                 guard_map[x][y] = '#';
  110.                 if(place_obstacle(visited, guard_map, x - steps[cur_dir][0], y - steps[cur_dir][1], (cur_dir + 1) % 4)){
  111.                     nobstacle_pos++;
  112.                 }
  113.                 guard_map[x][y] = 'X';
  114.             }
  115.             change_pos(x, y, cur_dir, '+');
  116.             if((quit = stop_check(x, y, nrows, ncols)))
  117.                 break;
  118.         }
  119.         change_pos(x, y, cur_dir, '-');
  120.         cur_dir = (cur_dir + 1) % 4;
  121.     }
  122.     std::cout << pos_visited << std::endl;
  123.     std::cout << nobstacle_pos << std::endl;
  124.     return 0;
  125. }
  126.  
Tags: aoc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement