Created
January 10, 2015 14:38
-
-
Save jmcph4/8db906ecbf7ba100a236 to your computer and use it in GitHub Desktop.
It's just past midnight and I'm tired. https://upload.wikimedia.org/wikipedia/en/7/76/Busy_Beaver_1.JPG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| // https://upload.wikimedia.org/wikipedia/en/7/76/Busy_Beaver_1.JPG | |
| class Turing | |
| { | |
| public: | |
| std::vector<bool> tape; // our tape, with an alphabet of {0,1} | |
| int state; // the machine's current state | |
| Turing::Turing() | |
| { | |
| // initialising the tape the lazy way | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->tape.push_back(false); | |
| this->state = 3; // starting state | |
| } | |
| int compute(int i) | |
| { | |
| if(i < this->tape.size()) // bounds check | |
| { | |
| if(this->state == 0) // halt | |
| { | |
| return 0; | |
| } | |
| else if(this->state == 1) // state A | |
| { | |
| if(this->tape[i] == false) | |
| { | |
| this->tape[i] = true; | |
| this->state = 2; | |
| compute(++i); // move right | |
| } | |
| else if(this->tape[i] == true) | |
| { | |
| this->state = 3; | |
| compute(--i); // move left | |
| } | |
| } | |
| else if(this->state == 2) // state B | |
| { | |
| if(this->tape[i] == false) | |
| { | |
| this->tape[i] = true; | |
| this->state = 1; | |
| compute(--i); | |
| } | |
| else if(this->tape[i] == true) | |
| { | |
| this->state = 2; | |
| compute(++i); | |
| } | |
| } | |
| else if(this->state == 3) // state C | |
| { | |
| if(this->tape[i] == false) | |
| { | |
| this->tape[i] = true; | |
| this->state = 2; | |
| compute(--i); | |
| } | |
| else if(this->tape[i] == true) | |
| { | |
| this->state = 0; | |
| compute(i); | |
| } | |
| } | |
| } | |
| } | |
| }; | |
| int main(void) | |
| { | |
| int i = 0; | |
| Turing t; | |
| t.compute(0); // go! | |
| // print tape after operations performed | |
| for(i=0;i<t.tape.size();i++) | |
| { | |
| std::cout << t.tape[i] << std::endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment