Advertisement
Guest User

--- Day 11: Seating System ---

a guest
Dec 11th, 2020
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4.  
  5. bool ApplyRulesP1(std::string& map, unsigned int width, unsigned int height)
  6. {
  7.     bool change = false;
  8.     std::string apply = map;
  9.    
  10.     for (unsigned int x = 0; x < width; x++)
  11.     {
  12.         for (unsigned int y = 0; y < height; y++)
  13.         {
  14.             char s = map[(y * width) + x];
  15.            
  16.             int occ = 0;
  17.             if (x > 0           &&                      map[(y * width) + (x - 1)] == '#') occ++;
  18.             if (x < width - 1   &&                      map[(y * width) + (x + 1)] == '#') occ++;
  19.             if (y > 0           &&                      map[((y - 1) * width) + x] == '#') occ++;
  20.             if (y < height - 1  &&                      map[((y + 1) * width) + x] == '#') occ++;
  21.             if (x > 0           && y > 0 &&             map[((y - 1) * width) + (x - 1)] == '#') occ++;
  22.             if (x < width - 1   && y > 0 &&             map[((y - 1) * width) + (x + 1)] == '#') occ++;
  23.             if (x > 0           && y < height - 1 &&    map[((y + 1) * width) + (x - 1)] == '#') occ++;
  24.             if (x < width - 1   && y < height - 1 &&    map[((y + 1) * width) + (x + 1)] == '#') occ++;
  25.            
  26.             if (s == 'L' && occ == 0)
  27.             {
  28.                 apply[(y * width) + x] = '#';
  29.                 change = true;
  30.             }
  31.             else if (s == '#' && occ >= 4)
  32.             {
  33.                 apply[(y * width) + x] = 'L';
  34.                 change = true;
  35.             }
  36.         }
  37.     }
  38.    
  39.     if (change)
  40.     {
  41.         map = apply;
  42.         return true;
  43.     }
  44.    
  45.     return change;
  46. }
  47.  
  48. bool FindAdjacentSeat(const std::string& map, int x, int y, int dirX, int dirY, unsigned int width, unsigned int height)
  49. {
  50.     x += dirX;
  51.     y += dirY;
  52.     while (x >= 0 && y >= 0 && x < width && y < height)
  53.     {
  54.         if (map[(y * width) + x] == '#')
  55.             return true;
  56.            
  57.         if (map[(y * width) + x] == 'L')
  58.             return false;
  59.        
  60.         x += dirX;
  61.         y += dirY;
  62.     }
  63.    
  64.     return false;
  65. }
  66.  
  67. bool ApplyRulesP2(std::string& map, unsigned int width, unsigned int height)
  68. {
  69.     bool change = false;
  70.     std::string apply = map;
  71.    
  72.     for (unsigned int x = 0; x < width; x++)
  73.     {
  74.         for (unsigned int y = 0; y < height; y++)
  75.         {
  76.             char s = map[(y * width) + x];
  77.            
  78.             int occ = 0;
  79.             if (FindAdjacentSeat(map, x, y, -1, 0, width, height)) occ++;
  80.             if (FindAdjacentSeat(map, x, y, +1, 0, width, height)) occ++;
  81.             if (FindAdjacentSeat(map, x, y, 0, -1, width, height)) occ++;
  82.             if (FindAdjacentSeat(map, x, y, 0, +1, width, height)) occ++;
  83.             if (FindAdjacentSeat(map, x, y, -1, -1, width, height)) occ++;
  84.             if (FindAdjacentSeat(map, x, y, +1, -1, width, height)) occ++;
  85.             if (FindAdjacentSeat(map, x, y, -1, +1, width, height)) occ++;
  86.             if (FindAdjacentSeat(map, x, y, +1, +1, width, height)) occ++;
  87.            
  88.             if (s == 'L' && occ == 0)
  89.             {
  90.                 apply[(y * width) + x] = '#';
  91.                 change = true;
  92.             }
  93.             else if (s == '#' && occ >= 5)
  94.             {
  95.                 apply[(y * width) + x] = 'L';
  96.                 change = true;
  97.             }
  98.         }
  99.     }
  100.    
  101.     if (change)
  102.     {
  103.         map = apply;
  104.         return true;
  105.     }
  106.    
  107.     return change;
  108. }
  109.  
  110. void PrintMap(const std::string& map, unsigned int width, unsigned int height)
  111. {
  112.     for (unsigned int y = 0; y < height; y++)
  113.     {
  114.         std::cout << map.substr(y * width, width) << std::endl;
  115.     }
  116. }
  117.  
  118. int main()
  119. {
  120.     unsigned int width = 0;
  121.     unsigned int height = 0;
  122.    
  123.     std::string map;
  124.     std::string text;
  125.     while (getline(std::cin, text))
  126.     {
  127.         if (height > 0) //Removes any \n or \0 from the last trail sequence
  128.             map.pop_back();
  129.        
  130.         map += text;
  131.         height++;
  132.         width = text.size();
  133.     }
  134.    
  135.     //PrintMap(map, width, height);
  136.     //std::cout << std::endl;
  137.     while (ApplyRulesP2(map, width, height))
  138.     {
  139.         //PrintMap(map, width, height);
  140.         //std::cout << std::endl;
  141.     }
  142.    
  143.     std::cout << "Number of occupied seats: " << std::count(map.begin(), map.end(), '#') << std::endl;
  144.    
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement