Guest User

AOC 24 Day 6 Part 2 v2 C++ Solution

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