Skip to content

Instantly share code, notes, and snippets.

@ChemiCalChems
Created January 19, 2017 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChemiCalChems/91caca7a02ad367eaa03c630c9869e9d to your computer and use it in GitHub Desktop.
Save ChemiCalChems/91caca7a02ad367eaa03c630c9869e9d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
enum Wire {white, black, purple, red, green, orange};
struct State {
virtual bool cut(Wire wire) {return true;}
};
struct WhiteCutState : State {
bool cut (Wire wire) {
if (wire == white || wire == black) {return false;}
return true;
}
};
struct RedCutState : State {
bool cut (Wire wire) {
if (wire == green) {return true;}
return false;
}
};
struct BlackCutState : State {
bool cut (Wire wire) {
if (wire == white || wire == green || wire == orange) {return false;}
return true;
}
};
struct OrangeCutState : State {
bool cut (Wire wire) {
if (wire == red || wire == black) {return true;}
return false;
}
};
struct GreenCutState : State {
bool cut (Wire wire) {
if (wire == orange || wire == white) {return true;}
return false;
}
};
struct PurpleCutState : State {
bool cut (Wire wire) {
if (wire == red || wire == black) {return true;}
return false;
}
};
inline State* getState(Wire wire) {
switch (wire) {
case white:
return new WhiteCutState;
case red:
return new RedCutState;
case black:
return new BlackCutState;
case orange:
return new OrangeCutState;
case green:
return new GreenCutState;
case purple:
return new PurpleCutState;
}
}
int main() {
std::vector<Wire> wires;
State* currentState = new State;
std::string line;
while (std::getline(std::cin, line) && !line.empty()) {
if (line == "white") {wires.push_back(white);}
else if (line == "red") {wires.push_back(red);}
else if (line == "black") {wires.push_back(black);}
else if (line == "orange"){wires.push_back(orange);}
else if (line == "green") {wires.push_back(green);}
else if (line == "purple"){wires.push_back(purple);}
}
for (Wire wire : wires) {
if (!currentState->cut(wire)) {
std::cout << "BOOM!" << std::endl;
return 0;
}
else {currentState = getState(wire);}
}
std::cout << "Bomb defused!" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment