Created
          April 17, 2015 07:53 
        
      - 
      
- 
        Save wallstop/d64f869b3aaecbb1dc1a to your computer and use it in GitHub Desktop. 
  
    
      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
    
  
  
    
  | template <class T> | |
| T& operator+=(T& lhs, const T& rhs) | |
| { | |
| for (typename T::size_type i = 0; i < lhs.size(); ++i) | |
| lhs[i] += rhs[i]; | |
| return lhs; | |
| } | |
| struct LoopResponse | |
| { | |
| const bool isLooping; | |
| const int numCycles; | |
| }; | |
| static LoopResponse determineLooping(const std::string& commandSequence) | |
| { | |
| std::array<int, 2> distanceTraveled; | |
| const static std::array<std::array<int, 2>, 4> directionModifications = { { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } } }; | |
| int direction = 0; | |
| const static int maxDirections = 4; | |
| for (auto const& character : commandSequence) | |
| { | |
| switch (character) | |
| { | |
| case 'S': | |
| distanceTraveled += directionModifications[direction]; | |
| break; | |
| case 'R': | |
| direction = (direction + 1) % maxDirections; | |
| break; | |
| case 'L': | |
| direction = (direction - 1) % maxDirections; | |
| break; | |
| } | |
| } | |
| // We loop if we haven't traveled any distance || if we're not facing north | |
| const bool traveledNoDistance = (distanceTraveled[0] == 0 && distanceTraveled[1] == 0); | |
| const bool isLooping = traveledNoDistance || direction != 0; | |
| const int numCycles = [&]() | |
| { | |
| if (traveledNoDistance) | |
| return 1; | |
| switch (direction) | |
| { | |
| // EAST || WEST -> 4 times | |
| case 1: | |
| case 3: | |
| return 4; | |
| // SOUTH -> 2 times | |
| case 2: | |
| return 2; | |
| } | |
| // Invalid | |
| return -1; | |
| }(); | |
| return{ isLooping, numCycles }; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment