Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <random>
- struct Task {
- static const int STATION_SIZE = 50;
- std::vector<bool> lights;
- int room;
- Task() {
- std::mt19937 generator((std::random_device()()));
- lights.resize(size_t(STATION_SIZE));
- for (int i = 0; i < STATION_SIZE; ++i) {
- lights[i] = std::uniform_int_distribution<>(0, 1)(generator) != 0;
- }
- room = 0;
- }
- void next() {
- room = room < STATION_SIZE - 1 ? room + 1 : 0;
- dump();
- }
- void previous() {
- room = room > 0 ? room - 1 : STATION_SIZE - 1;
- dump();
- }
- void dump() {
- printf("\r"); for(int i=0; i<STATION_SIZE; i++) printf("%d",lights[i]?1:0);
- //printf("\r"); for(int i=0; i<room; i++) printf(" "); printf("^\n");
- //system("pause");
- }
- bool isLightEnabled() {
- return lights[room];
- }
- void toggleLight() {
- lights[room] = !lights[room];
- dump();
- }
- void explore() {
- // DO NOT MODIFY FILE ABOVE THIS LINE
- // You should leave this function when you have disabled lights in all the station rooms
- // Use only 'next()', 'previous()', 'isLightEnabled()', 'toggleLight()', 'break', 'continue', 'do', 'else', 'for', 'if', 'return' and 'while'
- // No constants, defines, includes, lambdas, variables, templates, no other function calls except 4 functions above
- // No more than 1 KB of code
- // Solution should be resistant to source data changes, including 'STATION_SIZE', initial state of 'lights' vector and 'room' variable
- // check for special case (STATION_SIZE==1)
- if (!isLightEnabled()) toggleLight(); next(); toggleLight(); previous(); if (!isLightEnabled()) return;
- // light up 2 markers
- if (!isLightEnabled()) toggleLight(); next();
- if (!isLightEnabled()) toggleLight(); next();
- // pingpong
- for (;;) {
- while (!isLightEnabled()) next();
- toggleLight();
- while (!isLightEnabled()) previous();
- previous();
- if (!isLightEnabled()) break;
- next();
- next();
- }
- // cleanup
- next();
- toggleLight();
- // DO NOT MODIFY FILE BELOW THIS LINE
- }
- bool validate() {
- return std::find(lights.begin(), lights.end(), true) == lights.end();
- }
- };
- int main() {
- Task t;
- t.explore();
- if (t.validate()) {
- std::cout << "success\n";
- } else {
- std::cout << "failure\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement