Advertisement
joric

circularstation.cpp

Feb 15th, 2024
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <random>
  4.  
  5. struct Task {
  6.     static const int STATION_SIZE = 50;
  7.  
  8.     std::vector<bool> lights;
  9.     int room;
  10.  
  11.     Task() {
  12.         std::mt19937 generator((std::random_device()()));
  13.         lights.resize(size_t(STATION_SIZE));
  14.         for (int i = 0; i < STATION_SIZE; ++i) {
  15.             lights[i] = std::uniform_int_distribution<>(0, 1)(generator) != 0;
  16.         }
  17.  
  18.         room = 0;
  19.     }
  20.  
  21.     void next() {
  22.         room = room < STATION_SIZE - 1 ? room + 1 : 0;
  23.         dump();
  24.     }
  25.  
  26.     void previous() {
  27.         room = room > 0 ? room - 1 : STATION_SIZE - 1;
  28.         dump();
  29.     }
  30.  
  31.     void dump() {
  32.         printf("\r"); for(int i=0; i<STATION_SIZE; i++) printf("%d",lights[i]?1:0);
  33.         //printf("\r"); for(int i=0; i<room; i++) printf(" "); printf("^\n");
  34.         //system("pause");
  35.     }
  36.  
  37.     bool isLightEnabled() {
  38.         return lights[room];
  39.     }
  40.  
  41.     void toggleLight() {
  42.         lights[room] = !lights[room];
  43.         dump();
  44.     }
  45.  
  46.     void explore() {
  47.         // DO NOT MODIFY FILE ABOVE THIS LINE
  48.         // You should leave this function when you have disabled lights in all the station rooms
  49.         // Use only 'next()', 'previous()', 'isLightEnabled()', 'toggleLight()', 'break', 'continue', 'do', 'else', 'for', 'if', 'return' and 'while'
  50.         // No constants, defines, includes, lambdas, variables, templates, no other function calls except 4 functions above
  51.         // No more than 1 KB of code
  52.         // Solution should be resistant to source data changes, including 'STATION_SIZE', initial state of 'lights' vector and 'room' variable
  53.  
  54.  
  55.         // check for special case (STATION_SIZE==1)
  56.         if (!isLightEnabled()) toggleLight(); next(); toggleLight(); previous(); if (!isLightEnabled()) return;
  57.         // light up 2 markers
  58.         if (!isLightEnabled()) toggleLight(); next();
  59.         if (!isLightEnabled()) toggleLight(); next();
  60.         // pingpong
  61.         for (;;) {
  62.             while (!isLightEnabled()) next();
  63.             toggleLight();
  64.             while (!isLightEnabled()) previous();
  65.             previous();
  66.             if (!isLightEnabled()) break;
  67.             next();
  68.             next();
  69.         }
  70.         // cleanup
  71.         next();
  72.         toggleLight();
  73.  
  74.         // DO NOT MODIFY FILE BELOW THIS LINE
  75.     }
  76.  
  77.     bool validate() {
  78.         return std::find(lights.begin(), lights.end(), true) == lights.end();
  79.     }
  80. };
  81.  
  82. int main() {
  83.     Task t;
  84.  
  85.     t.explore();
  86.  
  87.     if (t.validate()) {
  88.         std::cout << "success\n";
  89.     } else {
  90.         std::cout << "failure\n";
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement